dom 4 jによるxml解析

20233 ワード

需要:xmlの一部のノードのkeyとvalueを取得し、javaのmapに変換します.まずxmlをdom 4 jのdocumentクラスに変換し,次に主にXPathを用いてノードを選択する.
ケース1: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;
}

ケース2: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;
}

dom 4 jにおけるDocumentHelperのparseText手法を用いてxmlをdom 4 jにおけるDocumentクラスに変換し,さらに解析を行う.
selectSingleNodeとselectNodeの2つのメソッドの違いは、上記の2つのxmlを参照してください.