XML数据解析(循环遍历解析)

前端之家收集整理的这篇文章主要介绍了XML数据解析(循环遍历解析)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。



package com.cdhy.hjzx.util;

import java.util.HashMap; import java.util.Iterator; import java.util.Map; import net.sf.json.JSONObject; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; /** * 解析类 * * @author Administrator * */ public class SaxXML { /**测试**/ // 解析开票成功后返回的xml字符串 /** * <?xml version="1.0" encoding="UTF-8" standalone="no"?> * <body><result>成功</result><djh>KP150817000066</djh><fplxdm>004</fplxdm> * <kpzt>正常发票</kpzt><fpdm>1110098076</fpdm><fphm>65341107</fphm></body> */ /** * <?xml version="1.0" encoding="gb2312" ?> <result generated="1216266428640" vo_id="getOperStatus"> <result><operStatus> <ch>0</ch><inUseC>0</inUseC><statusC>0</statusC><phone>_NA_</phone><ifLoginC>1</ifLoginC><oper>801</oper><setBusyC>false</setBusyC> </operStatus> </result> * @param ss * @return */ public static Map<String,String> parseResultXml(String ss) { Document doc = null; Map<String,String> map = new HashMap<String,String>(); try { doc = DocumentHelper.parseText(ss); // 将字符串转为XML Element employees = doc.getRootElement();// 根节点 for (Iterator i = employees.elementIterator(); i.hasNext();) {// 遍历下一节点 Element employee = (Element) i.next(); map.put(employee.getName(),employee.getText()); } } catch (DocumentException e) { e.printStackTrace(); } return map; } /*循环解析XML将数据以json对象的形式返回*/ public static JSONObject parseResultXml2(String ss){ Document doc = null; JSONObject jsob=new JSONObject(); try{ doc = DocumentHelper.parseText(ss); /*1,得到XML字符串根节点*/ Element empRoot =doc.getRootElement(); /*根节点内容*/ jsob.put("rootVal",empRoot.getTextTrim()); /*如果根节点有属性*/ if(empRoot.attributeCount()>0){ /*获取根节点属性属性属性名(key) 属性值(value)*/ for(int i=0;i<empRoot.attributeCount();i++){ /*获取根节点的属性名称属性值*/ jsob.put(empRoot.attribute(i).getName(),empRoot.attribute(i).getStringValue()); } } /*遍历根节点获取子节点对象*/ for(Iterator root=empRoot.elementIterator();root.hasNext();){ /*通过根节点遍历子节点*/ Element emp1 = (Element)root.next(); /*遍历一次字节点则取字节点相应的值 KEY 节点名称 VALUE 节点值保存到JSON中*/ jsob.put(emp1.getName(),emp1.getText()); /*遍历第二层中子节点*/ for(Iterator emp1s=emp1.elementIterator();emp1s.hasNext();){ /*遍历子节点内容*/ Element employee = (Element) emp1s.next(); /*节点名称和节点内容以KEY VALUE的方式保存到JSON对象*/ jsob.put(employee.getName(),employee.getText()); } } } catch(DocumentException e) { e.printStackTrace(); } System.out.println(jsob); return jsob; } //employees.attribute(0).getValue() public static void main(String[] args) { String xml="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"+ "<body><result>成功</result><djh>KP150817000066</djh><fplxdm>004</fplxdm>"+ "<kpzt>正常发票</kpzt><fpdm>1110098076</fpdm><fphm>65341107</fphm></body>"; String xml1="<?xml version=\"1.0\" encoding=\"gb2312\"?>"+ "<result generated=\"1216266428640\" vo_id=\"getOperStatus\">"+"成功"+"</result>"; String xml2="<?xml version=\"1.0\" encoding=\"gb2312\"?>"+ "<result generated=\"1216266428640\" vo_id=\"getOperStatus\"><operStatus>"+ "<ch>0</ch><inUseC>0</inUseC><statusC>0</statusC><phone>_NA_</phone><ifLoginC>1</ifLoginC><oper>801</oper><setBusyC>false</setBusyC>"+ "</operStatus></result>"; System.out.println(parseResultXml2(xml2).toString()); System.err.println(parseResultXml(xml).toString()); } } 原文链接:https://www.f2er.com/xml/295400.html

猜你在找的XML相关文章