利用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可知。