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();
}
}
} |
SaxReader讀取,更新xml檔案
本文轉自 matengbing 51CTO部落格,原文連結:http://blog.51cto.com/matengbing/1876374
相關文章
- Java系列:讀取XML檔案JavaXML
- C#讀取Xml檔案C#XML
- QDomDocument 讀取和編輯xml檔案XML
- nodejs xmlreader 讀寫xml檔案NodeJSXML
- php獲取xml檔案內容PHPXML
- 使用C#讀寫xml檔案C#XML
- go學習之檔案讀取問題(需更新)Go
- Unity 讀取xmlUnityXML
- 任意檔案讀取
- Java 讀取檔案Java
- ArcGIS切片服務獲取切片方案xml檔案(conf.xml)XML
- xml檔案XML
- IOC - 讀取配置檔案
- python 讀取文字檔案Python
- 前端讀取excel檔案前端Excel
- 用友任意檔案讀取
- viper 讀取配置檔案
- matlab讀取npy檔案Matlab
- python小白檔案讀取Python
- cocos讀取plist檔案
- go配置檔案讀取Go
- python讀取大檔案Python
- springboot讀取配置檔案Spring Boot
- Springboot整合MongoDB儲存檔案、讀取檔案Spring BootMongoDB
- 讀取檔案流並寫入檔案流
- 序列化篇 生成xml 以及讀取xmlXML
- go 讀取.ini配置檔案Go
- 01 讀取模板HTML檔案HTML
- pg從磁碟讀取檔案
- 6.1檔案下載、讀取
- 讀取資料夾檔案
- python如何讀取大檔案Python
- Mysql溯源-任意檔案讀取?MySql
- MATLAB快速讀取STL檔案Matlab
- java中讀取配置檔案Java
- go–讀取檔案的方式Go
- Spring之Property檔案讀取Spring
- Golang專案中讀取配置檔案Golang
- 如何用Python讀取xml檔案後,裁剪標註圖片和擴容資料PythonXML