利用dom4j解析xml
来源:互联网 发布:全职太太 知乎 编辑:程序博客网 时间:2024/04/28 18:03
需求:得到xml中某些节点的key和value,转为java中的map。
先将xml转为dom4j中的document类,接着主要是运用XPath来选取节点。
情况一:
取xml文件中listUserInfoResp节点中的key和value
<?xml version="1.0" encoding="utf-8"?><response> <errno>0</errno> <errmsg/> <erraction/> <listUserInfoResp> <strName/> <strAddr/> <strTel/> <strEmail/> <strID/> <strAccounts>123@163.com</strAccounts> <strRemark/> <strUserType>1</strUserType> <strUserCreateTime>14622131312</strUserCreateTime> <strMaxDownRate/> <strMaxUpRate/> <strUserID>AA0216180813815</strUserID> <strUserPass/> <strCardNo/> <strCardKey/> <strStratGrpdl>02</strStratGrpdl> </listUserInfoResp></response>
解析:
public Map<String, String> mockUserInfo(){ Map<String, String> userInfo = new HashMap<String, String>(); String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><response><errno>0</errno><errmsg/><erraction/><listUserInfoResp><strName/><strAddr/><strTel/><strEmail/><strID/><strAccounts>123@163.com</strAccounts><strRemark/><strUserType>1</strUserType><strUserCreateTime>1462961323</strUserCreateTime><strMaxDownRate/><strMaxUpRate/><strUserID>AA0216180813815</strUserID><strUserPass/><strCardNo/><strCardKey/><strStratGrpdl>02</strStratGrpdl></listUserInfoResp></response>"; try { Document doc = DocumentHelper.parseText(xml); Node nodes = doc.selectSingleNode("//listUserInfoResp"); List<Element> userInfoList = ((Element) nodes).elements(); for(int i = 0; i < userInfoList.size(); i ++){ userInfo.put(userInfoList.get(i).getName(), userInfoList.get(i).getStringValue()); System.out.println(userInfoList.get(i).getName() + ": " + userInfoList.get(i).getStringValue()); } return userInfo; } catch (DocumentException e) { e.printStackTrace(); } return null;}
情况二:
取xml中listServiceInfoResp的strName和iStatus的内容,组成map
<?xml version="1.0" encoding="utf-8"?><response> <errno>0</errno> <errmsg/> <erraction/> <listServiceInfoResps> <listServiceInfoResp> <strName>csej</strName> <iStatus>0</iStatus> <iOpenTime/> </listServiceInfoResp> <listServiceInfoResp> <strName>dband</strName> <iStatus>0</iStatus> <iOpenTime/> </listServiceInfoResp> <listServiceInfoResp> <strName>iad</strName> <iStatus>0</iStatus> <iOpenTime/> </listServiceInfoResp> <listServiceInfoResp> <strName>iptv</strName> <iStatus>0</iStatus> <iOpenTime/> </listServiceInfoResp> <listServiceInfoResp> <strName>pbx</strName> <iStatus>0</iStatus> <iOpenTime/> </listServiceInfoResp> <listServiceInfoResp> <strName>voip</strName> <iStatus>1</iStatus> <iOpenTime/> </listServiceInfoResp> <listServiceInfoResp> <strName>wband</strName> <iStatus>1</iStatus> <iOpenTime/> </listServiceInfoResp> <listServiceInfoResp> <strName>wifi</strName> <iStatus>0</iStatus> <iOpenTime/> </listServiceInfoResp> </listServiceInfoResps></response>
解析:
public Map<String, String> mockBussinessList(){ Map<String, String> bussinessList = new HashMap<String, String>(); try { String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><response><errno>0</errno><errmsg/><erraction/><listServiceInfoResps><listServiceInfoResp><strName>csej</strName><iStatus>0</iStatus><iOpenTime/></listServiceInfoResp><listServiceInfoResp><strName>dband</strName><iStatus>0</iStatus><iOpenTime/></listServiceInfoResp><listServiceInfoResp><strName>iad</strName><iStatus>0</iStatus><iOpenTime/></listServiceInfoResp><listServiceInfoResp><strName>iptv</strName><iStatus>0</iStatus><iOpenTime/></listServiceInfoResp><listServiceInfoResp><strName>pbx</strName><iStatus>0</iStatus><iOpenTime/></listServiceInfoResp><listServiceInfoResp><strName>voip</strName><iStatus>1</iStatus><iOpenTime/></listServiceInfoResp><listServiceInfoResp><strName>wband</strName><iStatus>1</iStatus><iOpenTime/></listServiceInfoResp><listServiceInfoResp><strName>wifi</strName><iStatus>0</iStatus><iOpenTime/></listServiceInfoResp></listServiceInfoResps></response>"; Document doc = DocumentHelper.parseText(xml); List<Node> names = doc.selectNodes("//strName"); List<Node> values = doc.selectNodes("//iStatus"); for(int i = 0; i < names.size(); i ++){ bussinessList.put(names.get(i).getStringValue(), values.get(i).getStringValue()); System.out.println(names.get(i).getStringValue() + ": " + values.get(i).getStringValue()); } } catch (DocumentException e) { e.printStackTrace(); } return bussinessList;}
利用dom4j中DocumentHelper的parseText方法将xml转为dom4j中的Document类,进而进行解析。
selectSingleNode和selectNode两种方法的区别,查看上述两种xml可知。
阅读全文
0 0
- 利用dom4j解析XML
- 利用dom4j解析xml
- 利用dom4j解析xml
- 利用dom4j解析xml文件
- 利用Dom4j解析xml文档
- 利用dom4j解析xml--天气预报
- 利用Dom4j 解析XML文件
- 利用dom4j创建XML和解析XML
- 利用dom4j创建XML和解析XML
- 利用dom4j解析 Google sitemap.xml
- 《JavaWeb----利用dom4j解析xml文件》
- 利用dom4j进行XML解析实例
- 利用JAXP和DOM4J解析XML
- java 利用dom4j 解析XML文件
- 利用dom4j编写XML解析器
- 利用dom4j解析.xml之查询标签
- 利用dom4j解析.xml之查询属性
- java利用dom4j解析XML文件
- Java的JAR包, EAR包 ,WAR包 都是干什么的,有什么区别
- 微信小程序 使用LeanCloud
- IDL波段分解与合成源代码
- HDU2444 The Accomodation of Students
- 面试干货
- 利用dom4j解析xml
- phpstudy如何配置本地站点
- 10.8-全栈Java笔记:序列化/反序列化的步骤和实例
- 文件下载功能
- 一道上机试题(某时刻正在运行任务的个数)
- git使用攻略
- Tensorflow的高级封装
- java poi读取excel文件
- /bin,/sbin,/usr/sbin,/usr/bin 目录之简单区别