XML資料解析(迴圈遍歷解析)
阿新 • • 發佈:2019-01-23
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());
}
}