資源簡介
該資源包含了java四種方式讀取和寫入xml文件的代碼

代碼片段和文件信息
package?com.domread.test;
import?java.io.FileInputStream;
import?java.io.FileNotFoundException;
import?java.io.FileOutputStream;
import?java.io.IOException;
import?java.io.InputStream;
import?java.io.UnsupportedEncodingException;
import?java.util.Iterator;
import?java.util.List;
import?org.dom4j.Attribute;
import?org.dom4j.Document;
import?org.dom4j.DocumentException;
import?org.dom4j.DocumentHelper;
import?org.dom4j.Element;
import?org.dom4j.io.OutputFormat;
import?org.dom4j.io.SAXReader;
import?org.dom4j.io.xmlWriter;
public?class?Dom4JTest?{
public?static?void?main(String[]?args)?{
writexmlByDom4J();
// readxmlByDom4J();
}
private?static?void?writexmlByDom4J()?{
try?{
//?創建文檔并設置文檔的根元素節點???
Element?root?=?DocumentHelper.createElement(“peoples“);??
Document?document?=?DocumentHelper.createDocument(root);??
//根節點??
root.addAttribute(“name““peoplesvalue“);??
//子節點??
Element?element1?=?root.addElement(“people“);??
element1.addAttribute(?“id“?“1“?);
element1.addElement(“name“?“小王“);?
element1.addElement(“age““22“);
Element?element2?=?root.addElement(“people“);??
element2.addAttribute(?“id“?“2“?);
element2.addElement(“name“?“老王“);?
element2.addElement(“age““23“);
//添加??
xmlWriter?xmlwriter2?=?new?xmlWriter();??
xmlwriter2.write(document);??
//創建文件??
OutputFormat?format?=?new?OutputFormat();??
format?=?OutputFormat.createPrettyPrint();
//設定編碼
format.setEncoding(“UTF-8“);
xmlWriter?xmlwriter?=?new?xmlWriter(new?FileOutputStream(“d:/dom4jpeople.xml“)?format);
xmlwriter.write(document);
xmlwriter.flush();
}?catch?(FileNotFoundException?e)?{
e.printStackTrace();
}?catch?(UnsupportedEncodingException?e)?{
e.printStackTrace();
}?catch?(IOException?e)?{
e.printStackTrace();
}??
}??
private?static?void?readxmlByDom4J()?{
try?{
//?1.?創建org.dom4j.io.SAXReader對象
SAXReader?saxReader?=?new?SAXReader();
InputStream?ins?=?new?FileInputStream(“people.xml“);
Document?document?=??saxReader.read(ins);
Element?rootElement?=?document.getRootElement();
System.out.println(“根節點的名稱是:“+rootElement.getName());
Iterator?iterator?=?rootElement.elementIterator();
while?(iterator.hasNext())?{
Element?element?=?(Element)?iterator.next();
//?獲取所有屬性名和屬性值
ListattributesList?=?element.attributes();
for?(Attribute?attribute?:?attributesList)?{
System.out.println(“屬性名:“+attribute.getName()+“======屬性值“+attribute.getValue());
}
//?遍歷子節點
Iterator?childIterator?=?element.elementIterator();
while?(childIterator.hasNext())?{
Element?child?=?(Element)?childIterator.next();
System.out.println(“屬性名:“+child.getName()+“======屬性值“+child.getStringValue());
}
}
}?catch?(FileNotFoundException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}?catch?(DocumentException?e)?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2017-02-27?22:26??domtest\
?????文件?????????404??2017-02-27?19:40??domtest\.classpath
?????文件?????????383??2017-02-26?12:11??domtest\.project
?????目錄???????????0??2017-02-27?22:26??domtest\.settings\
?????文件?????????598??2017-02-26?12:11??domtest\.settings\org.eclipse.jdt.core.prefs
?????目錄???????????0??2017-02-27?22:26??domtest\bin\
?????目錄???????????0??2017-02-27?22:26??domtest\bin\com\
?????目錄???????????0??2017-02-27?22:26??domtest\bin\com\domread\
?????目錄???????????0??2017-02-27?22:26??domtest\bin\com\domread\test\
?????文件????????4111??2017-02-27?22:23??domtest\bin\com\domread\test\Dom4JTest.class
?????文件????????4858??2017-02-27?22:18??domtest\bin\com\domread\test\DomTest.class
?????文件????????3721??2017-02-27?22:20??domtest\bin\com\domread\test\JDomTest.class
?????文件????????2460??2017-02-27?21:05??domtest\bin\com\domread\test\SaxTest$SelfHandler.class
?????文件????????1329??2017-02-27?21:05??domtest\bin\com\domread\test\SaxTest.class
?????目錄???????????0??2017-02-27?22:26??domtest\bin\com\domwrite\
?????目錄???????????0??2017-02-27?22:26??domtest\bin\com\domwrite\test\
?????文件?????????391??2017-02-27?21:13??domtest\bin\com\domwrite\test\Test.class
?????文件?????????402??2017-02-27?21:15??domtest\bin\com\domwrite\test\Writexm
?????文件??????313359??2017-02-27?19:40??domtest\dom4j-1.6.jar
?????文件??????153253??2017-02-26?13:29??domtest\jdom_1_0.jar
?????文件?????????179??2017-02-27?21:08??domtest\newxm
?????文件?????????273??2017-02-26?12:14??domtest\people.xm
?????文件?????????211??2017-02-27?22:18??domtest\people2.xm
?????文件?????????336??2017-02-27?22:22??domtest\people3.xm
?????文件?????????249??2017-02-27?22:23??domtest\people4.xm
?????目錄???????????0??2017-02-27?22:26??domtest\src\
?????目錄???????????0??2017-02-27?22:26??domtest\src\com\
?????目錄???????????0??2017-02-27?22:26??domtest\src\com\domread\
?????目錄???????????0??2017-02-27?22:26??domtest\src\com\domread\test\
?????文件????????3158??2017-02-27?22:23??domtest\src\com\domread\test\Dom4JTest.java
?????文件????????4303??2017-02-27?22:18??domtest\src\com\domread\test\DomTest.java
............此處省略6個文件信息
- 上一篇:cglib所需要的所有jar包
- 下一篇:回音噪音處理Demo.apk
評論
共有 條評論