package com.yc.test.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;
import com.thoughtworks.xstream.io.xml.XppDriver;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.*;
import java.io.Writer;
import java.util.List;
public class XmlUtils {
private static Document strToDocument(String xml) throws DocumentException {
return DocumentHelper.parseText(xml);
}
public static JSONObject documentToJSONObject(String xml) {
JSONObject jsonObject = null;
try {
jsonObject = elementToJSONObject(strToDocument(xml).getRootElement());
} catch (DocumentException e) {
e.printStackTrace();
} finally {
return jsonObject;
}
}
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;
}
private static XStream createXstream() {
XStream xstream = new XStream(new MyXppDriver(false));
xstream.autodetectAnnotations(true);
return xstream;
}
public static String toXML(Object obj, Class<?> cls) {
if (obj == null) {
return null;
}
XStream xstream = createXstream();
xstream.processAnnotations(cls);
return getDefaultXMLHeader() + xstream.toXML(obj);
}
public static String toXML(Object obj) {
if (obj == null) {
return null;
}
XStream xstream = createXstream();
return getDefaultXMLHeader() + xstream.toXML(obj);
}
@SuppressWarnings("unchecked")
public static <T> T xml2Obj(String xml, Class<?> cls) {
if (StringUtils.isBlank(xml)) {
return null;
}
XStream xstream = createXstream();
if (cls != null) {
xstream.processAnnotations(cls);
}
return (T) xstream.fromXML(xml);
}
public static <T> T xml2Obj(String xml) {
return xml2Obj(xml, null);
}
private static String getDefaultXMLHeader() {
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
}
public static class MyXppDriver extends XppDriver {
boolean useCDATA = false;
MyXppDriver(boolean useCDATA) {
super(new XmlFriendlyNameCoder("__", "_"));
this.useCDATA = useCDATA;
}
@Override
public HierarchicalStreamWriter createWriter(Writer out) {
if (!useCDATA) {
return super.createWriter(out);
}
return new PrettyPrintWriter(out) {
boolean cdata = true;
@Override
public void startNode(String name, @SuppressWarnings("rawtypes") Class clazz) {
super.startNode(name, clazz);
}
@Override
protected void writeText(QuickWriter writer, String text) {
if (cdata) {
writer.write(cDATA(text));
} else {
writer.write(text);
}
}
private String cDATA(String text) {
return "<![CDATA[" + text + "]]>";
}
};
}
}
}