package com.service.zl.model;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class XmlTools {
public static Document strToDocument(String xml) throws DocumentException {
return DocumentHelper.parseText(xml);
}
public static JSONObject documentToJSONObject(String xml) throws DocumentException {
return elementToJSONObject(strToDocument(xml).getRootElement());
}
public static JSONObject elementToJSONObject(Element node) {
JSONObject result = new JSONObject();
List<Attribute> listAttr = node.attributes();
for (Attribute attr : listAttr) {
result.put(attr.getName(), attr.getValue());
}
List<Element> listElement = node.elements();
if (!listElement.isEmpty()) {
for (Element e : listElement) {
if (e.attributes().isEmpty() && e.elements().isEmpty())
result.put(e.getName(), e.getTextTrim());
else {
if (!result.containsKey(e.getName()))
result.put(e.getName(), new JSONArray());
((JSONArray) result.get(e.getName())).add(elementToJSONObject(e));
}
}
}
return result;
}
}