HTML文檔
1 import javax.xml.parsers.DocumentBuilder;
2 import javax.xml.parsers.DocumentBuilderFactory;
3 import javax.xml.transform.Transformer;
4 import javax.xml.transform.TransformerFactory;
5 import javax.xml.transform.dom.DOMSource;
6 import javax.xml.transform.stream.StreamResult;
7 import org.w3c.dom.Document;
8 import org.w3c.dom.Element;
9 import org.w3c.dom.Node;
10 import org.w3c.dom.NodeList;
11
12 public class DomDemolianxi {
13 public static void main(String[] args) {
14
15 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
16 DocumentBuilder builder = null;
17 Document doc = null;
18 try {
19 builder = factory.newDocumentBuilder();
20 doc = builder.parse("src/book.xml");
21 //===============================================
22 // test1(doc);
23 // test2(doc);
24 test3(doc);
25 // test4(doc);
26 // test5(doc);
27 // test6(doc);
28 // test7(doc);
29 // test8(doc);
30 //===============================================
31 } catch (Exception e) {
32 e.printStackTrace();
33 }
34 }
35
36 // 1.擷取第二本書的作者
37 public static void test1(Document doc){
38 NodeList nl = doc.getElementsByTagName("作者");
39 System.out.println(nl.item(1).getTextContent());
40 }
41
42 // 2.擷取全部元素的名稱
43 public static void test2(Node d){
44 if(d.getNodeType()==Node.ELEMENT_NODE){
45 System.out.println(d.getNodeName());
46 }
47 NodeList nl = d.getChildNodes();
48 for(int i=0;i<nl.getLength();i++){
49 Node d1 = nl.item(i);
50 test2(d1);
51 }
52 }
53
54 // 3.把zhd的書售價改為10元
55 public static void test3(Document doc) throws Exception{
56 NodeList nl = doc.getElementsByTagName("售價");
57 nl.item(1).setTextContent("10元");
58 //把記憶體中dom樹寫回xml
59 TransformerFactory factory = TransformerFactory.newInstance();
60 Transformer transformer = factory.newTransformer();
61 transformer.transform(new DOMSource(doc),new StreamResult("src/book.xml"));
62 }
63
64 // 4.加一個子節點 第二本書 内部價 5角
65 public static void test4(Document doc) throws Exception{
66 NodeList nl = doc.getElementsByTagName("書");
67 Node secondBook = nl.item(1);
68 Element e = doc.createElement("内部價");
69 e.setTextContent("7元");
70 secondBook.appendChild(e);
71 //把記憶體中dom樹寫回xml
72 TransformerFactory factory = TransformerFactory.newInstance();
73 Transformer transformer = factory.newTransformer();
74 transformer.transform(new DOMSource(doc),new StreamResult("src/book.xml"));
75 }
76
77 // 5.在第二本書 售價的前面增加批發價 5元
78 public static void test5(Document doc) throws Exception{
79 Node son = doc.getElementsByTagName("售價").item(1);
80 Node dad = son.getParentNode();
81 Element e = doc.createElement("批發價");
82 e.setTextContent("5元");
83 dad.insertBefore(e, son);
84 //把記憶體中dom樹寫回xml
85 TransformerFactory factory = TransformerFactory.newInstance();
86 Transformer transformer = factory.newTransformer();
87 transformer.transform(new DOMSource(doc),new StreamResult("src/book.xml"));
88 }
89
90 // 6.删除第二本書的内部價
91 public static void test6(Document doc) throws Exception{
92 Node son = doc.getElementsByTagName("内部價").item(0);
93 Node dad = son.getParentNode();
94 dad.removeChild(son);
95 //把記憶體中dom樹寫回xml
96 TransformerFactory factory = TransformerFactory.newInstance();
97 Transformer transformer = factory.newTransformer();
98 transformer.transform(new DOMSource(doc),new StreamResult("src/book.xml"));
99 }
100
101 // 7.擷取第一本書的出版社
102 public static void test7(Document doc) throws Exception{
103 Element nl =(Element) doc.getElementsByTagName("書").item(0);
104 String n =nl.getAttribute("出版社");
105 System.out.println(n);
106 }
107
108 // 8.給第二本書加上出版社(屬性)
109 public static void test8(Document doc) throws Exception{
110 Element nl = (Element)doc.getElementsByTagName("書").item(1);
111 nl.setAttribute("出版社", "人民出版社");
112 //把記憶體中dom樹寫回xml
113 TransformerFactory factory = TransformerFactory.newInstance();
114 Transformer transformer = factory.newTransformer();
115 transformer.transform(new DOMSource(doc),new StreamResult("src/book.xml"));
116 }
117
118
119 }
XML文檔:
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?><書架>
2 <書 id="1" 出版社="wwg日報">
3 <書名>傳奇人生</書名>
4 <作者>wwg</作者>
5 <售價>999.0元</售價>
6 </書>
7 <書 id="2" 出版社="人民出版社">
8 <書名>論吃</書名>
9 <作者>zhd</作者>
10 <批發價>5元</批發價>
11 <售價>10元</售價>
12
13 </書>
14 </書架>