基于dom4j解析xml字符串为实体对象

闲话不多说,直接开始正题,利用dom4j解析xml格式的字符串,然后将对应的节点映射成实体对象。

传入的xml格式字符串的样例

对于节点属性,我简写了,即使很多也是一样的。

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
String sXml ="<?xml version='1.0' encoding='UTF-8'?>"
+ "<ContractRoot>"
+ "<TcpCont>"
+ " <TransactionID>1000000001201612110000000010</TransactionID>"
+ " <Request>"
+ " <ReqType>0</ReqType>"
+ " </Request>"
+ "</TcpCont>"
+ "<SvcCont>"
+ " <bictitem>"
+ " <itemid>40000011666306</itemid>"
+ " </bictitem>"
+ " <ictsubitems>"
+ " <ictsubitem>"
+ " <id>1</id>"
+ " <wbs_list>"
+ " <btype>微信支付</btype>"
+ " </wbs_list>"
+ " <wbs_list>"
+ " <btype>qq支付</btype>"
+ " </wbs_list>"
+ " <cost_list>"
+ " <btype>付费业务</btype>"
+ " </cost_list>"
+ " </ictsubitem>"
+ " </ictsubitems>"
+ "</SvcCont>"
+ "</ContractRoot>";

怎么用呢?

我的是基于maven项目的,在xml里面需要加入依赖:

1
2
3
4
5
6
<!--解析xml格式的字符串 -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.1</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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//需要引入的包
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.InputSource;
public class TestDom {
public static void main(String[] args) {
String sXml ="<?xml version='1.0' encoding='UTF-8'?>"
+ "<ContractRoot>"
+ "<TcpCont>"
+ " <TransactionID>1000000001201612110000000010</TransactionID>"
+ " <Request>"
+ " <ReqType>0</ReqType>"
+ " </Request>"
+ "</TcpCont>"
+ "<SvcCont>"
+ " <bictitem>"
+ " <itemid>40000011666306</itemid>"
+ " </bictitem>"
+ " <ictsubitems>"
+ " <ictsubitem>"
+ " <id>1</id>"
+ " <wbs_list>"
+ " <btype>微信支付</btype>"
+ " </wbs_list>"
+ " <wbs_list>"
+ " <btype>qq支付</btype>"
+ " </wbs_list>"
+ " <cost_list>"
+ " <btype>付费业务</btype>"
+ " </cost_list>"
+ " </ictsubitem>"
+ " </ictsubitems>"
+ "</SvcCont>"
+ "</ContractRoot>";
Document doc = null;
try {
//如果这里的xml是上传的来的,不是直接传递的字符串,那么我们需要接收
// SAXReader就是一个管道,用一个流的方式,把xml文件读出来
// SAXReader reader = new SAXReader(); //User.hbm.xml表示你要解析的xml文档
// Document document = reader.read(new File("User.hbm.xml"));
//将字符串转为XML
doc = DocumentHelper.parseText(sXml);
//获取根节点
Element rootElt = doc.getRootElement();
//拿到根节点的名称
System.out.println("根节点:" + rootElt.getName());
//获取根节点下的子节点
Iterator<Element> tcpCont = rootElt.elementIterator("TcpCont");
Iterator<Element> svcCont = rootElt.elementIterator("SvcCont");
Tcpcont addTcpcont = new Tcpcont();
while (tcpCont.hasNext()) {
Element tcpContEl = (Element) tcpCont.next();
//将节点保存到数据实体中。。
addTcpcont.setId(UUIDutils.getUUID());
//TransactionID要和节点的名字保持一致
addTcpcont.setTransactionid(tcpContEl.elementTextTrim("TransactionID"));
addTcpcont.setActioncode(tcpContEl.elementTextTrim("ActionCode"));
addTcpcont.setBuscode(tcpContEl.elementTextTrim("BusCode"));
addTcpcont.setServicecode(tcpContEl.elementTextTrim("ServiceCode"));
addTcpcont.setServicecontractver(tcpContEl.elementTextTrim("ServiceContractVer"));
addTcpcont.setServicelevel(tcpContEl.elementTextTrim("ServiceLevel"));
addTcpcont.setSrcorgid(tcpContEl.elementTextTrim("SrcOrgID"));
addTcpcont.setSrcsysid(tcpContEl.elementTextTrim("SrcSysID"));
addTcpcont.setSrcsyssign(tcpContEl.elementTextTrim("SrcSysSign"));
addTcpcont.setDstorgid(tcpContEl.elementTextTrim("DstOrgID"));
addTcpcont.setDstsysid(tcpContEl.elementTextTrim("DstSysID"));
addTcpcont.setReqtime(tcpContEl.elementTextTrim("ReqTime"));
Iterator<Element> request = tcpContEl.elementIterator("Request");
System.out.println("request="+request);
while(request.hasNext()){
Element elementRequest = (Element) request.next();
System.out.println("ReqType="+elementRequest.elementTextTrim("ReqType"));
}
}
while(svcCont.hasNext()){
Element elementGroupService = (Element) svcCont.next();
Iterator<Element> bictitem = elementGroupService.elementIterator("bictitem");
Iterator<Element> ictsubitems = elementGroupService.elementIterator("ictsubitems");
while(ictsubitems.hasNext()){
Element element = (Element) ictsubitems.next();
Iterator<Element> ictsubitem = element.elementIterator("ictsubitem");
Iterator<Element> wbs_list = element.elementIterator("wbs_list");
Iterator<Element> cost_list = element.elementIterator("cost_list");
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
}

实体类:

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
public class Tcpcont {
private String id;
private String transactionid;
private String actioncode;
private String buscode;
private String servicecode;
private String servicecontractver;
private String servicelevel;
private String srcorgid;
private String srcsysid;
private String srcsyssign;
private String dstorgid;
private String dstsysid;
private String reqtime;
//.......省略getter 和setter方法
}
坚持原创技术分享,您的支持将鼓励我继续创作!

分享
Fork me on GitHub