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 = RootElement();// 根节点
for (Iterator i = employees.elementIterator(); i.hasNext();) {// 遍历下⼀节点
Element employee = (Element) i.next();
map.Name(), Text());
}
} 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 =RootElement();
/*根节点内容*/
jsob.put("rootVal", TextTrim());
/*如果根节点有属性*/
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 = (();
/*遍历⼀次字节点则取字节点相应的值 KEY 节点名称 VALUE 节点值保存到JSON中*/ jsob.Name(), Text());
/*遍历第⼆层中⼦节点*/
for(Iterator emp1s=emp1.elementIterator();emp1s.hasNext();){
/*遍历⼦节点内容*/
Element employee = (Element) ();
/*节点名称和节点内容以KEY VALUE的⽅式保存到JSON对象*/
jsob.Name(), Text());
}
}
}
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());
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论