DOM4J例子详解



DOM4J例子详解

最近熟悉了一下Dom4,写了一些小代码,分为生成与解析。
一、生成xml
需要的jar包:dom4j、jaxen
输出的结果xml
<?xml version=”1.0″ encoding=”gb2312″?>

<SOAP-ENV:Envelope xmlns:SOAP-ENV=”http://schemas.xmlsoap.org/soap/envelope/” xmlns:SOAP-ENC=”http://schemas.xmlsoap.org/soap/encoding/” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
<!–Envelope Comment–><?target text?>

<SOAP-ENV:Body>
<m:InsertCP xmlns:m=”http://tempuri.org/”>Hello world!</m:InsertCP>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
java代码
1package org.xmltool.test;
2
3import java.io.FileWriter;
4import java.io.IOException;
5
6import org.dom4j.Document;
7import org.dom4j.DocumentHelper;
8import org.dom4j.Element;
9import org.dom4j.Namespace;
10import org.dom4j.QName;
11import org.dom4j.io.OutputFormat;
12import org.dom4j.io.XMLWriter;
13
14public class Test {
15
16
20 public static void main(String[] args) throws IOException {
21
22
23 Document doc = DocumentHelper.createDocument();
24
25
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
36 QName envelopeQName = new QName(“Envelope”, SOAP_ENV_ns);
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
46 QName bodyQName = new QName(“Body”, SOAP_ENV_ns);
47 Element bodyElement = DocumentHelper.createElement_x(bodyQName);
48 envelopeElement.add(bodyElement);
49
50
51 Namespace m_ns = new Namespace(“m”, “http://tempuri.org/”);
52 QName insertCpQName = new QName(“InsertCP”, m_ns);
53 Element InsertCpElement = DocumentHelper.createElement_x(insertCpQName);
54 bodyElement.add(InsertCpElement);
55
56 OutputFormat format = OutputFormat.createPrettyPrint();
57 // OutputFormat format = OutputFormat.createCompactFormat();
59 format.setEncoding(“gb2312″);
60 XMLWriter writer = new XMLWriter(new FileWriter(“c:/test.xml”), format);
61 writer.write(doc);
62 writer.close();
63 }
64}
65

二、解析xml
采用了Visitor模式。通过遍历XML的各个节点,完成XML解析。
1package org.xmltool.bean;
2
3import org.dom4j.Document;
4import org.dom4j.DocumentException;
5import org.dom4j.Element;
6import org.dom4j.VisitorSupport;
7import org.dom4j.io.SAXReader;
8
9public 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
执行结果为:
/SOAP-ENV:Envelope
/SOAP-ENV:Envelope/SOAP-ENV:Body
/SOAP-ENV:Envelope/SOAP-ENV:Body/m:InsertCP
Hello world!

——————–