package com.mall.utils;
/**
- @ClassName:
- @Date: 2019-05
- @Author:Yang
*/
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import java.util.*;
public class XMLTools {
/**
* @param map
* @return
*/
public static String mapToXml(Map map) {
Document document = DocumentHelper.createDocument();
Element nodeElement = document.addElement("xml");
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String key = entry.getKey().toString();
String value = "";
if (entry.getValue() != null) {
value = entry.getValue().toString();
}
Element element = nodeElement.addElement(key);
if (key.equalsIgnoreCase("detail")) {
element.addText("<![CDATA[" + value + "]]>");
} else {
element.addText(value);
}
}
return docToString(nodeElement);
}
/**
* @param list
* @return
* @throws Exception
*/
public static String listToXml(List list) throws Exception {
Document document = DocumentHelper.createDocument();
Element nodesElement = document.addElement("nodes");
int i = 0;
for (Object o : list) {
Element nodeElement = nodesElement.addElement("node");
if (o instanceof Map) {
for (Object obj : ((Map) o).keySet()) {
Element keyElement = nodeElement.addElement("key");
keyElement.addAttribute("label", String.valueOf(obj));
keyElement.setText(String.valueOf(((Map) o).get(obj)));
}
} else {
Element keyElement = nodeElement.addElement("key");
keyElement.addAttribute("label", String.valueOf(i));
keyElement.setText(String.valueOf(o));
}
i++;
}
return docToString(nodesElement);
}
/**
* @param xml
* @return
*/
public static Map xmlToMap(String xml) {
try {
Map map = new HashMap();
Document document = DocumentHelper.parseText(xml);
Element nodeElement = document.getRootElement();
List node = nodeElement.elements();
for (Iterator it = node.iterator(); it.hasNext(); ) {
Element elm = (Element) it.next();
map.put(elm.getName(), elm.getText());
}
return map;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @param xml
* @return
*/
public static List xmlToList(String xml) {
try {
List<Map> list = new ArrayList<Map>();
Document document = DocumentHelper.parseText(xml);
Element nodesElement = document.getRootElement();
List nodes = nodesElement.elements();
for (Iterator its = nodes.iterator(); its.hasNext(); ) {
Element nodeElement = (Element) its.next();
Map map = xmlToMap(nodeElement.asXML());
list.add(map);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @param document
* @return
*/
public static String docToString(Element document) {
String s = "";
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputFormat format = new OutputFormat(" ", true, "UTF-8");
XMLWriter writer = new XMLWriter(out, format);
writer.write(document);
s = out.toString("UTF-8");
out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return s;
}
}