dom4j解析xml文档(增删改查)

时间:2022-10-21 00:20:01
dom4j解析xml文档(增删改查)dom4j解析xml文档(增删改查)
 1 package itcast.dom4j;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.util.List;
8
9 import org.dom4j.Document;
10 import org.dom4j.DocumentException;
11 import org.dom4j.DocumentHelper;
12 import org.dom4j.Element;
13 import org.dom4j.io.OutputFormat;
14 import org.dom4j.io.SAXReader;
15 import org.dom4j.io.XMLWriter;
16 import org.junit.Test;
17
18 public class Demo {
19
20
21
22 // 在第一本书上添加一个新的售价:<price>209yuan</price>
23 @Test
24 public void add() throws Exception {
25 SAXReader reader = new SAXReader();
26 Document document = reader.read(new File("src/book.xml"));
27
28 Element book = document.getRootElement().element("书");
29 book.addElement("price").setText("209yuan");
30
31 OutputFormat format = OutputFormat.createPrettyPrint();
32 format.setEncoding("UTF-8");
33
34 XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),
35 format);
36 writer.write(document);
37 writer.close();
38 }
39
40 @Test
41 public void add2() throws Exception {
42 SAXReader reader = new SAXReader();
43 Document document = reader.read(new File("src/book.xml"));
44
45 Element book = document.getRootElement().element("书");
46 List list = book.elements();// bookname/author/price
47
48 Element price = DocumentHelper.createElement("price");
49 price.setText("309yuan");
50
51 list.add(2, price);
52
53 OutputFormat format = OutputFormat.createPrettyPrint();
54 format.setEncoding("UTF-8");
55
56 XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),
57 format);
58 writer.write(document);
59 writer.close();
60 }
61
62 @Test
63 public void delete() throws Exception {
64 SAXReader reader = new SAXReader();
65 Document document = reader.read(new File("src/book.xml"));
66
67 Element price = document.getRootElement().element("书").element("price");
68 price.getParent().remove(price);
69
70 OutputFormat format = OutputFormat.createPrettyPrint();
71 format.setEncoding("UTF-8");
72
73 XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),
74 format);
75 writer.write(document);
76 writer.close();
77 }
78
79 @Test
80 public void update() throws Exception {
81 SAXReader reader = new SAXReader();
82 Document document = reader.read(new File("src/book.xml"));
83
84 Element book = (Element) document.getRootElement().elements("书").get(1);
85 book.element("editor").setText("zero");
86
87 OutputFormat format = OutputFormat.createPrettyPrint();
88 format.setEncoding("UTF-8");
89
90 XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),
91 format);
92 writer.write(document);
93 writer.close();
94 }
95
96 }
View Code
dom4j解析xml文档(增删改查)dom4j解析xml文档(增删改查)
 1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <书架>
4 <>
5 <bookname name="zero">java jiuyepeixunjiaocheng</bookname>
6 <editor>zhangxiaoxiang</editor>
7 <price>209yuan</price>
8 </>
9 <>
10 <bookname name="one">javaWebprogramming</bookname>
11 <editor>zero</editor>
12 <price>28.00yuan</price>
13 </>
14 </书架>
View Code