dom4j解析XML简单方法

<?xml version="1.0" encoding="UTF-8"?>
<data>
	<message>
		<status>0</status>
		<value>处理成功</value>
	</message>
	<policeCheckInfos>
		<policeCheckInfo name="李佳" id="325826128609280830">
			<message>
				<status>0</status>
				<value>查询成功</value>
			</message>
			<name desc="姓名">李佳</name>
			<identitycard desc="身份证号">325826128609280830</identitycard>
			<compStatus desc="比对状态">3</compStatus>
			<compResult desc="比对结果">一致</compResult>
			<checkPhoto desc="照片">
                                /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0a
				HBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIy
				I60GNSfOQyBVT5m/ee1QCFQ/c8UNzUj/AHDWhmQyIkY+6KdDbLN+8yR9KSA+YPmqbH+j4pEDREzH
				5W/Omkyg9j9BT06VJ/yzpouxB50n/PD+VFWh0opDsf/Z</checkPhoto>
			<no desc="唯一标识" />
		</policeCheckInfo>
	</policeCheckInfos>
</data>

解析后放入HashMap中供调用

	/*
	 * 接口返回XML,得到HaspMap
	 */
	public static HashMap AnalyticXML(String xml){
		HashMap<String,String> hm = new HashMap<String,String>();				 
		try {
			// 读取并解析XML文档
            // SAXReader就是一个管道,用一个流的方式,把xml文件读出来 
            // SAXReader reader = new SAXReader(); //User.hbm.xml表示你要解析的xml文档
			// Document document = reader.read(new File("User.hbm.xml"));
			
	         Document doc = DocumentHelper.parseText(xml); //加载XML字符串,得到Document对象
	         Element root=doc.getRootElement();  //获得根节点
	         
	         Node processStatus = doc.selectSingleNode("/data/message/status");
	         String ProcessStatus = processStatus.getText(); //处理结果码
	         Node processValue = doc.selectSingleNode("/data/message/value");
	         String ProcessValue = processValue.getText();   //处理结果描述
	         hm.put("ProcessStatus", ProcessStatus);
	         hm.put("ProcessValue", ProcessValue);
	         
	         if(ProcessStatus.equals("0")){
	        	 List policeCheckInfoList = (List)doc.selectNodes("/data/policeCheckInfos/policeCheckInfo");
		         for(int i=0;i<policeCheckInfoList.size();i++){
		        	 Element policeCheckInfo = (Element)policeCheckInfoList.get(i);	        	 
		        	 String CheckName = policeCheckInfo.attributeValue("name");   //请求认证的人员姓名
			         String CheckId = policeCheckInfo.getTextTrim();	//请求认证的人员证件号
			         hm.put("CheckName", CheckName);
			         hm.put("CheckId", CheckId);
			         
			         Element queryStatus = (Element)doc.selectSingleNode("/data/policeCheckInfos/policeCheckInfo/message/status");
			         String QueryStatus = queryStatus.getTextTrim(); //查询结果码
			         Element queryValue = (Element)doc.selectSingleNode("/data/policeCheckInfos/policeCheckInfo/message/value");
			         String QueryValue = queryValue.getTextTrim(); //查询结果描述
			         hm.put("QueryStatus", QueryStatus);
			         hm.put("QueryValue", QueryValue);
			         Element eleName = (Element)doc.selectSingleNode("/data/policeCheckInfos/policeCheckInfo/name");
			         String NameN = eleName.attributeValue("desc");	//认证反馈姓名描述
			         String NameV = eleName.getTextTrim(); //认证反馈姓名值
			         hm.put("NameN", NameN);
			         hm.put("NameV", NameV);
			         Element eleIdentityCard = (Element)doc.selectSingleNode("/data/policeCheckInfos/policeCheckInfo/identitycard");
			         String IdentityCardN = eleIdentityCard.attributeValue("desc");	 //认证反馈身份证号描述
			         String IdentityCardV = eleIdentityCard.getTextTrim();  //认证反馈身份证号值
			         hm.put("IdentityCardN", IdentityCardN);
			         hm.put("IdentityCardV", IdentityCardV);
			         Element eleCompStatus = (Element)doc.selectSingleNode("/data/policeCheckInfos/policeCheckInfo/compStatus");
			         String CompStatusN = eleCompStatus.attributeValue("desc");	 //认证反馈比对状态描述
			         String CompStatusV = eleCompStatus.getTextTrim();  //认证反馈比对状态号值
			         hm.put("CompStatusN", CompStatusN);
			         hm.put("CompStatusV", CompStatusV);
			         Element eleCompResult = (Element)doc.selectSingleNode("/data/policeCheckInfos/policeCheckInfo/compResult");
			         String CompResultN = eleCompResult.attributeValue("desc");	 //认证反馈比对结果描述
			         String CompResultV = eleCompResult.getTextTrim();  //认证反馈比对结果值
			         hm.put("CompResultN", CompResultN);
			         hm.put("CompResultV", CompResultV);
			         String CheckPhotoN = "";
			         String CheckPhotoV = "";		         
			         if(CompStatusV.equals("3")){
			        	 Element eleCheckPhoto = (Element)doc.selectSingleNode("/data/policeCheckInfos/policeCheckInfo/checkPhoto");
				         CheckPhotoN = eleCheckPhoto.attributeValue("desc");	 //认证反馈照片描述
				         CheckPhotoV = eleCheckPhoto.getTextTrim();  //认证反馈照片值      
				         hm.put("CheckPhotoN", CheckPhotoN);
				         hm.put("CheckPhotoV", CheckPhotoV);
			         }
			         Element eleNo = (Element)doc.selectSingleNode("/data/policeCheckInfos/policeCheckInfo/no");
			         String NoN = eleNo.attributeValue("desc");  //认证反馈唯一标识描述
			         String NoV = eleNo.getTextTrim();  //认证反馈唯一标识值     
			         hm.put("NoN", NoN);
			         hm.put("NoV", NoV);
		         }
	         }else{
	        	 hm.put("CheckName", "");
		         hm.put("CheckId", "");
		         hm.put("QueryStatus", "");
		         hm.put("QueryValue", "");
		         hm.put("NameN", "");
		         hm.put("NameV", "");
		         hm.put("IdentityCardN", "");
		         hm.put("IdentityCardV", "");
		         hm.put("CompStatusN", "");
		         hm.put("CompStatusV", "");
		         hm.put("CompResultN", "");
		         hm.put("CompResultV", "");
		         hm.put("CheckPhotoN", "");
		         hm.put("CheckPhotoV", "");
		         hm.put("NoN", "");
		         hm.put("NoV", "");
	         }	         
		} catch (DocumentException e) {			
			e.printStackTrace();
		}		
		return hm;
	}

小结:

1).获取 doc

Node processStatus = doc.selectSingleNode("/data/message/status");

Element eleName = (Element)doc.selectSingleNode("/data/policeCheckInfos/policeCheckInfo/name");

获得 doc 后,即可用 selectSingleNode 方法加层次路径获取 Node 或 Element;

2).获得 属性值和参数值

String CheckName = policeCheckInfo.attributeValue("name");  

获得了 Node 或 Element 后,可使用 attributeValue 方法获得属性值;

String CheckId = policeCheckInfo.getTextTrim();

获得了 Node 或 Element 后,可使用 getTextTrim 方法获得参数值;

3).当内部有多组参数时,借助List,使用 selectNodes 方法

List policeCheckInfoList = (List)doc.selectNodes("/data/policeCheckInfos/policeCheckInfo");
            for(int i=0;i<policeCheckInfoList.size();i++){

Element policeCheckInfo = (Element)policeCheckInfoList.get(i);   //获取此层次的属性值

Element queryStatus = (Element)doc.selectSingleNode("/data/policeCheckInfos/policeCheckInfo/message/status");//

            }

与获取子节点寻找父节点的方法相比,上述方法直观简便。我觉得更好用。


你可能感兴趣的:(xml,list,String,HashMap,encoding,照片)