最近熟悉了一下Dom4,寫了一些小代碼,分為生成與解析。
一、生成xml
需要的jar包:dom4j、jaxen
輸出的結果xml










java代碼
1
package org.xmltool.test;
2
3
import java.io.FileWriter;
4
import java.io.IOException;
5
6
import org.dom4j.Document;
7
import org.dom4j.DocumentHelper;
8
import org.dom4j.Element;
9
import org.dom4j.Namespace;
10
import org.dom4j.QName;
11
import org.dom4j.io.OutputFormat;
12
import org.dom4j.io.XMLWriter;
13
14
public class Test {
15
16
/**
17
* @param args
18
* @throws IOException
19
*/
20
public static void main(String[] args) throws IOException {
21
22
/* 創建Document */
23
Document doc = DocumentHelper.createDocument();
24
25
/* 創建備用Namespace */
26
Namespace SOAP_ENV_ns = new Namespace("SOAP-ENV",
27
"http://schemas.xmlsoap.org/soap/envelope/");
28
Namespace SOAP_ENC_ns = new Namespace("SOAP-ENC",
29
"http://schemas.xmlsoap.org/soap/encoding/");
30
Namespace xsi_ns = new Namespace("xsi",
31
"http://www.w3.org/2001/XMLSchema-instance");
32
Namespace xsd_ns = new Namespace("xsd",
33
"http://www.w3.org/2001/XMLSchema");
34
35
/* SOAP-ENV:Envelope */
36
QName envelopeQName = new QName("Envelope", SOAP_ENV_ns);/* 構造帶名字空間頭信息SOAP-ENV的節點Envelope */
37
Element envelopeElement = doc.addElement(envelopeQName);
38
envelopeElement.addComment("Envelope Comment");/* 添加注釋 */
39
envelopeElement.addProcessingInstruction("target", "text");/* 添加指令 */
40
/* 添加其它名字空間(作為屬性) */
41
envelopeElement.add(SOAP_ENC_ns);
42
envelopeElement.add(xsi_ns);
43
envelopeElement.add(xsd_ns);
44
45
/* SOAP-ENV:Body */
46
QName bodyQName = new QName("Body", SOAP_ENV_ns);
47
Element bodyElement = DocumentHelper.createElement(bodyQName);
48
envelopeElement.add(bodyElement);
49
50
/* m:InsertCP */
51
Namespace m_ns = new Namespace("m", "http://tempuri.org/");
52
QName insertCpQName = new QName("InsertCP", m_ns);
53
Element InsertCpElement = DocumentHelper.createElement(insertCpQName);
54
bodyElement.add(InsertCpElement);
55
56
OutputFormat format = OutputFormat.createPrettyPrint();/* 美化格式的輸出 */
57
// OutputFormat format = OutputFormat.createCompactFormat();/*
58
// 壓縮格式的輸出,節省空間 */
59
format.setEncoding("gb2312");/* xml的編碼 */
60
XMLWriter writer = new XMLWriter(new FileWriter("c:/test.xml"), format);
61
writer.write(doc);
62
writer.close();/* 必寫無疑 */
63
}
64
}
65

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

二、解析xml
采用了Visitor模式。通過遍歷XML的各個節點,完成XML解析。
1
package org.xmltool.bean;
2
3
import org.dom4j.Document;
4
import org.dom4j.DocumentException;
5
import org.dom4j.Element;
6
import org.dom4j.VisitorSupport;
7
import org.dom4j.io.SAXReader;
8
9
public class MyVisitor extends VisitorSupport {
10
@Override
11
public void visit(Element node) {
12
System.out.println(node.getPath());
13
if ("InsertCP".equals(node.getName())) {
14
System.out.println(node.getText());
15
}
16
}
17
18
public static void main(String[] args) throws DocumentException {
19
SAXReader reader = new SAXReader();
20
Document doc = reader.read("c:/test.xml");
21
doc.accept(new MyVisitor());
22
}
23
}
24

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

執行結果為:




--------------------
WE準高手