08-XMLUtil

XMLUtil - XML文件 生成&解析

  • 依赖
1
2
3
4
5
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.3</version>
</dependency>
  • 工具类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.SAXWriter;

import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class XmlUtil {

/**
* 生成 XML
* @return 字符串
*/
public static String createXML(Map<String, String> map) {
//StringBuffer buffer=new StringBuffer();
//buffer.append("<xml>");
//for(String k:map.keySet()){
// buffer.append("<"+k+">"+map.get(k)+"</"+k+">");
//}
//buffer.append("</xml>");
//return buffer.toString();
Document document = DocumentHelper.createDocument();
Element root = document.addElement("xml");
Set<String> keys = map.keySet();
for (String k : keys) {
Element child = root.addElement(k);
//child.setText("<![CDATA["+map.get(k)+"]]");
child.setText(map.get(k));
}
return document.asXML();
}

/**
* 解析
* @return Map<String, String>
*/
public static Map<String, String> parseXml(String xml) {
SAXReader reader = new SAXReader();
try {
Document document = reader.read(new ByteArrayInputStream(xml.getBytes()));
//获取根节点
Element root = document.getRootElement();
List<Element> list = root.elements();
Map<String, String> map = new HashMap<>();
for (Element e : list) {
map.put(e.getName(), e.getTextTrim());
}
return map;
} catch (DocumentException e) {
e.printStackTrace();
}
return null;
}
}


08-XMLUtil
https://janycode.github.io/2016/05/03/21_代码片段/01_工具类/08-XMLUtil/
作者
Jerry(姜源)
发布于
2016年5月3日
许可协议