利用dom4j解析xml

需求:得到xml中某些节点的key和value,转为java中的map。
先将xml转为dom4j中的document类,接着主要是运用XPath来选取节点。

情况一:
取xml文件中listUserInfoResp节点中的key和value



<response>
  <errno>0errno>
  <errmsg/>
  <erraction/>
  <listUserInfoResp>
    <strName/>
    <strAddr/>
    <strTel/>
    <strEmail/>
    <strID/>
    <strAccounts>[email protected]strAccounts>
    <strRemark/>
    <strUserType>1strUserType>
    <strUserCreateTime>14622131312strUserCreateTime>
    <strMaxDownRate/>
    <strMaxUpRate/>
    <strUserID>AA0216180813815strUserID>
    <strUserPass/>
    <strCardNo/>
    <strCardKey/>
    <strStratGrpdl>02strStratGrpdl>
  listUserInfoResp>
response>

解析:

public Map<String, String> mockUserInfo(){
        Map<String, String> userInfo = new HashMap<String, String>();
        String xml = "<response><errno>0errno><errmsg/><erraction/><listUserInfoResp><strName/><strAddr/><strTel/><strEmail/><strID/><strAccounts>[email protected]strAccounts><strRemark/><strUserType>1strUserType><strUserCreateTime>1462961323strUserCreateTime><strMaxDownRate/><strMaxUpRate/><strUserID>AA0216180813815strUserID><strUserPass/><strCardNo/><strCardKey/><strStratGrpdl>02strStratGrpdl>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



<response>
  <errno>0errno>
  <errmsg/>
  <erraction/>
  <listServiceInfoResps>
    <listServiceInfoResp>
      <strName>csejstrName>
      <iStatus>0iStatus>
      <iOpenTime/>
    listServiceInfoResp>
    <listServiceInfoResp>
      <strName>dbandstrName>
      <iStatus>0iStatus>
      <iOpenTime/>
    listServiceInfoResp>
    <listServiceInfoResp>
      <strName>iadstrName>
      <iStatus>0iStatus>
      <iOpenTime/>
    listServiceInfoResp>
    <listServiceInfoResp>
      <strName>iptvstrName>
      <iStatus>0iStatus>
      <iOpenTime/>
    listServiceInfoResp>
    <listServiceInfoResp>
      <strName>pbxstrName>
      <iStatus>0iStatus>
      <iOpenTime/>
    listServiceInfoResp>
    <listServiceInfoResp>
      <strName>voipstrName>
      <iStatus>1iStatus>
      <iOpenTime/>
    listServiceInfoResp>
    <listServiceInfoResp>
      <strName>wbandstrName>
      <iStatus>1iStatus>
      <iOpenTime/>
    listServiceInfoResp>
    <listServiceInfoResp>
      <strName>wifistrName>
      <iStatus>0iStatus>
      <iOpenTime/>
    listServiceInfoResp>
  listServiceInfoResps>
response>

解析:

public Map<String, String> mockBussinessList(){
        Map<String, String> bussinessList = new HashMap<String, String>();
        try {
            String xml = "<response><errno>0errno><errmsg/><erraction/><listServiceInfoResps><listServiceInfoResp><strName>csejstrName><iStatus>0iStatus><iOpenTime/>listServiceInfoResp><listServiceInfoResp><strName>dbandstrName><iStatus>0iStatus><iOpenTime/>listServiceInfoResp><listServiceInfoResp><strName>iadstrName><iStatus>0iStatus><iOpenTime/>listServiceInfoResp><listServiceInfoResp><strName>iptvstrName><iStatus>0iStatus><iOpenTime/>listServiceInfoResp><listServiceInfoResp><strName>pbxstrName><iStatus>0iStatus><iOpenTime/>listServiceInfoResp><listServiceInfoResp><strName>voipstrName><iStatus>1iStatus><iOpenTime/>listServiceInfoResp><listServiceInfoResp><strName>wbandstrName><iStatus>1iStatus><iOpenTime/>listServiceInfoResp><listServiceInfoResp><strName>wifistrName><iStatus>0iStatus><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可知。

你可能感兴趣的:(java)