XML格式化及解析
⼀个解析xml 成map的简单的⼯具类,只⽀持如下如下格式化的xml
1<object>
2<;栏⽬>李树鹏</栏⽬>
3<request_day>22</request_day>
4<day>22</day>
5<name>tttt</name>
6</object>
XMLValueObject类的全部代码:
1import java.io.StringReader;
2import java.util.ArrayList;
3import java.util.LinkedHashMap;
4import java.util.List;
5import java.util.Map;
6import org.apachemons.lang.StringUtils;
7import org.apache.log4j.Logger;
8import org.dom4j.Document;
9import org.dom4j.DocumentException;
10import org.dom4j.DocumentHelper;
11import org.dom4j.Element;
12import org.dom4j.io.SAXReader;
13
14public class XMLValueObject implements java.io.Serializable {
15
16private static final long serialVersionUID = -3163892375347045824L;
17
18private static Logger logger = Logger(XMLValueObject.class);
19
20private Map<String, List<String>> map;
21
22public XMLValueObject() {
23 map = new LinkedHashMap<String, List<String>>();
24 }
25
26public void put(String key, String value) {
27if (value == null) {
28return;
29 }
30
31 List<String> list = (key);
32if (list == null) {
33 list = new ArrayList<String>();
34 map.put(key, list);
35 }
36 veAll(list);
37 list.add(value);
38 }
39
40public void put(String key, String[] values) {
41if (values == null || values.length == 0) {
42return;
43 }
44
45 List<String> list = (key);
46if (list == null) {
47 list = new ArrayList<String>();
48 map.put(key, list);
49 }
50for (String value : values) {
51 list.add(value);
dom4j读取xml52 }
53 }
54
55public Object get(String key) {
56 List<String> list = (key);
57return list == null || list.isEmpty() ? null : (0);
58 }
59
60public Object[] getArray(String key) {
61 List<String> list = (key);
62return list == null ? new String[0] : Array(new String[0]);
63 }
64
65 @SuppressWarnings("unchecked")
66public void fromXML(String xml) {
67try{
68if (StringUtils.isEmpty(xml)) {
69return;
70 }
71 StringReader in = new StringReader(xml);
72 SAXReader reader = new SAXReader(false);
73 Document doc = null;
74try {
75 doc = ad(in);
76 }
77catch (DocumentException e) {
78 ("Could not read XML[" + xml + "]", e);
79 }
80finally {
81 in.close();
82 }
83
84 Element root = RootElement();
85 List<Element> children = (List<Element>) root.elements();
86for (Element child : children) {
87 Name(), Text());
88 }
89
90 }catch(Exception e)
91 {
92//System.out.print("错误"+e.getMessage()+":"+xml);
93 e.printStackTrace();
94 }
95
96 }
97
98public String toXML() {
99 Document doc = ateDocument();
100 Element root = doc.addElement("object");
101//root.addAttribute("class", Name());
102
103for (Map.Entry<String, List<String>> entry : Set()) {
104 String key = Key();
105 List<String> list = Value();
106
107for (String value : list) {
108 Element child = root.addElement(key);
109 child.addText(value);
110 }
111 }
112
113return doc.asXML();
114 }
115
116public Map<String, List<String>> getMap() {
117return map;
118 }
119
120public void setMap(Map<String, List<String>> map) {
121this.map = map;
122 }
123 }
测试代码:
1public static void main(String[] args) {
2 XMLValueObject vo = new XMLValueObject();
3 vo.fromXML("<object><;栏⽬>李树鹏</栏⽬><request_day>22</request_day><day>22</day><name>tttt</name></object>");
4 System.out.("栏⽬"));
5 vo.put("temp", "1212");
6 System.out.XML());
7 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论