需求:得到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>[email protected]</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>[email protected]</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可知。