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
package com.sun.xml;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
 
 
 
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
 
public class ReadXmlBySaxReader {
    public static void main(String[] args) throws IOException {
        String path="C:\\Users\\Administrator\\Desktop\\bookstore.xml";
        Document document=getDocument(path);
        getNode(document);
        updateEle("aaaa", document,path);
        addEle(document, "程式設計書籍", path);
    }
    public static Document getDocument(String path) throws UnsupportedEncodingException, FileNotFoundException{
        SAXReader saxReader=new SAXReader();
        File file=new File(path);
        Document document=null;
        InputStreamReader inputStreamReader=new InputStreamReader(new FileInputStream(file));
        try {
            document=saxReader.read(inputStreamReader);
            return document;
        catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
     
    public static void getNode(Document document){
        Element element=document.getRootElement();
        @SuppressWarnings("unchecked")
        List<Element> list=element.elements("book");
        Iterator<Element> iterator=list.iterator();
        while(iterator.hasNext()){
            Element node=iterator.next();
            Element element2=node.element("title");
            System.out.println(element2.getText());
        }
        System.out.println("ss");
    }
    public static void updateEle(String text,Document document,String path) throws IOException{
        Element element=document.getRootElement();
        @SuppressWarnings("unchecked")
        List<Element> list=element.elements("book");
        Iterator<Element> iterator=list.iterator();
        while(iterator.hasNext()){
            Element node=iterator.next();
            Element element2=node.element("title");
            System.out.println("title為:   "+element2.getText());
            if(element2.getText().equals("ss")){
                element2.setText("java書籍");
            }
             
        }
        writeXml(document,path);
    }
     
     
    public static void addEle(Document document,String text,String path) throws IOException{
        Element rootElement=document.getRootElement();
        Element element=rootElement.addElement("book");
        Element element2=element.addElement("title");
        Element element3=element.addElement("author");
        Element element4=element.addElement("price");
        element2.setText(text);
        element3.setText("萬福");
        element4.setText("39.0");
        writeXml(document, path);
    }
    public static void writeXml(Document document,String path) throws IOException{
        OutputFormat outputFormat=OutputFormat.createPrettyPrint();
         
        try {
            XMLWriter xmlWriter=(XMLWriter) new XMLWriter(new FileOutputStream(path),outputFormat);
            xmlWriter.write(document);
            xmlWriter.close();
        catch (UnsupportedEncodingException | FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}