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
- 讀取xml檔案 解析雙層xmlXML
- Java系列:讀取XML檔案JavaXML
- C#讀取Xml檔案C#XML
- 用JDOM讀取XML檔案XML
- XMl 檔案屬性的讀取XML
- NSXMLDocument讀取遠端XML檔案XML
- 用dom4j SAXReader解析xml檔案及字串XML字串
- QDomDocument 讀取和編輯xml檔案XML
- Java讀取多層級xml檔案JavaXML
- Rss-Rome 閱讀器讀取XML檔案XML
- javascript讀取xml檔案程式碼例項JavaScriptXML
- 文摘:在EJB中讀取XML配置檔案XML
- js讀取xml檔案,用了Microsoft.XMLDOMJSXMLROS
- 簡單讀取XML檔案中的值XML
- Sql server:從XML檔案中讀取配置資訊SQLServerXML
- SQL Server中讀取XML檔案的簡單做法SQLServerXML
- 讀寫iOS XML檔案iOSXML
- go學習之檔案讀取問題(需更新)Go
- php獲取xml檔案內容PHPXML
- 使用C#讀寫xml檔案C#XML
- Java 讀取檔案Java
- tiff檔案讀取
- jquery實現的讀取並解析xml檔案程式碼例項jQueryXML
- Java讀取以.xlsx結尾的excel檔案,並寫出每張表對應的c#類、java類、儲存資料的xml檔案、讀取xml檔案的工具類JavaExcelC#XML
- python讀取檔案——python讀取和儲存mat檔案Python
- 如何實時讀取一個不斷更新的文字檔案
- nodejs xmlreader 讀寫xml檔案NodeJSXML
- dom4j讀寫xml檔案XML
- C#讀取XMLC#XML
- viper 讀取配置檔案
- go配置檔案讀取Go
- iOS讀取.csv檔案iOS
- php 讀取超大檔案PHP
- WinForm讀取Excel檔案ORMExcel
- java讀取properties檔案Java
- 用友任意檔案讀取
- VB讀取文字檔案的例子:逐行讀取