在java環境下讀取xml文件的方法主要有4種:DOM、SAX、JDOM、JAXB
1. DOM(Document Object Model)
此 方法主要由W3C提供,它將xml文件全部讀入內存中,然後將各個元素組成一棵數據樹,以便快速的訪問各個節點 。 因此非常消耗系統性能 ,對比較大的文檔不適宜採用DOM方法來解析。 DOM API 直接沿襲了 XML 規範。每個結點都可以擴展的基於 Node 的接口,就多態性的觀點來講,它是優秀的,但是在 Java 語言中的應用不方便,並且可讀性不強。
實例:
import javax.xml.parsers.*;
//XML解析器接口
import org.w3c.dom.*;
//XML的DOM實現
import org.apache.crimson.tree.XmlDocument;
//寫XML文件要用到
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//允許名字空間
factory.setNamespaceAware(true);
//允許驗證
factory.setValidating(true);
//獲得DocumentBuilder的一個實例
try {
DocumentBuilder builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException pce) {
System.err.println(pce);
// 出異常時輸出異常信息,然後退出,下同
System.exit(1);
}
//解析文檔,並獲得一個Document實例。
try {
Document doc = builder.parse(fileURI);
} catch (DOMException dom) {
System.err.println(dom.getMessage());
System.exit(1);
} catch (IOException ioe) {
System.err.println(ioe);
System.exit(1);
}
//獲得根節點StuInfo
Element elmtStuInfo = doc.getDocumentElement();
//得到所有student節點
NodeList nlStudent = elmtStuInfo.getElementsByTagNameNS(
strNamespace, "student");
for (……){
//當前student節點元素
Element elmtStudent = (Element)nlStudent.item(i);
NodeList nlCurrent = elmtStudent.getElementsByTagNameNS(
strNamespace, "name");
}
對於讀取得方法其實是很簡單的,寫入xml文件也是一樣不復雜。
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory .newDocumentBuilder();
} catch (ParserConfigurationException pce) {
System.err.println(pce);
System.exit(1);
}
Document doc = null;
doc = builder .newDocument();
//下面是建立XML文檔內容的過程,
//先建立根元素"學生花名冊"
Element root = doc.createElement("學生花名冊");
//根元素添加上文檔
doc.appendChild(root);
//建立"學生"元素,添加到根元素
Element student = doc.createElement("學生");
student.setAttribute("性別", studentBean.getSex());
root.appendChild(student);
//建立"姓名"元素,添加到學生下面,下同
Element name = doc.createElement("姓名");
student.appendChild(name);
Text tName = doc.createTextNode(studentBean.getName());
name.appendChild(tName);
Element age = doc.createElement("年齡");
student.appendChild(age);
Text tAge = doc.createTextNode(String.valueOf(studentBean.getAge()));
age.appendChild(tAge);
2.SAX (Simple API for XML)
此方法主要由XML-DEV 郵件列表的成員開發的,SAX是基於事件的方法,它很類似於標籤庫的處理機制,在標籤開始、結束以及錯誤發生等等地方調用相應的接口實現方法,不是全部文 檔都讀入內存。 SAX具有優異的性能和利用更少的存儲空間特點。SAX 的設計只考慮了功能的強大性,卻沒有考慮程序員使用起來是否方便。
使用必須擴展ContentHandler、ErrorHandler、DTDHandler等,但是必須擴展ContentHandler(或者DefaultHandler )。
import org.xml.sax.*;
public class MyContentHandler implements ContentHandler {
… …
}
/**
* 當其他某一個調用事件發生時,先調用此方法來在文檔中定位。
* @param locator
*/
public void setDocumentLocator(Locator locator){
}
/**
* 在解析整個文檔開始時調用
* @throws SAXException
*/
public void startDocument() throws SAXException{
System.out.println("** Student information start **");
}
/**
* 在解析整個文檔結束時調用
* @throws SAXException
*/
public void endDocument() throws SAXException{
System.out.println("**** Student information end ****");
}
/**
* 在解析名字空間開始時調用
* @param prefix
* @param uri
* @throws SAXException
*/
public void startPrefixMapping(String prefix
, String uri) throws SAXException{
}
/**
* 在解析名字空間結束時調用
* @param prefix
* @throws SAXException
*/
public void endPrefixMapping(String prefix) throws SAXException{
}
/**
* 在解析元素開始時調用
* @param namespaceURI
* @param localName
* @param qName
* @param atts
* @throws SAXException
*/
public void startElement(String namespaceURI, String localName
, String qName, Attributes atts) throws SAXException{
}
/** 在解析元素結束時調用
* @param namespaceURI
* @param localName 本地名,如student
* @param qName 原始名,如LIT:student
* @throws SAXException */
public void endElement(String namespaceURI, String localName,String qName) throws SAXException{
if (localName.equals(“student”)){
System.out.println(localName+":"+currentData);
}
}
取得元素數據的方法——characters
取得元素數據中的空白的方法——ignorableWhitespace
在解析到處理指令時調用的方法——processingInstruction
當未驗證解析器忽略實體時調用的方法——skippedEntity
運行時,只需要使用下列代碼:
MySAXParser mySAXParser = new MySAXParser();
mySAXParser.parserXMLFile("SutInfo.xml");
3.JDOM
JDOM的處理方式有些類似於DOM,但它主要是用SAX實現的 。JDOM用Java的數據類型來定義操作數據樹的各個節點 。JDOM的性能也很優越。
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
SAXBuilder builder = new SAXBuilder(false);
//得到Document
Document doc = builder.build(fileURI);
//名字空間
Namespace ns = Namespace.getNamespace("LIT" , "http://www.lit.edu.cn/student/ ");
//取得所有LIT:student節點的集合
List lstStudents = elmtStuInfo.getChildren("student",ns);
for ( … ){
Element elmtStudent = (Element)lstStudents.get(i);
elmtStudent.getChildTextTrim("name", ns);
}
//修改
elmtLesson.getChild("lessonScore" , ns).setText("100");
//刪除
elmtStuInfo.removeChild("master", ns);
//添加
elmtStuInfo.addContent(new Element("master" , ns).addContent(new Entity("masterName")));
//輸出文檔
//第一個參數是縮進字符串,這裏是4個空格。
//第二個參數是true,表示需要換行。
XMLOutputter printDoc = new XMLOutputter(" ", true);
printDoc.output(doc, new FileOutputStream("StuInfo.xml"));
4.JAXB (Java And XML Binding)
JAXB 是以SUN爲主的一些公司公佈的。JAXB將schema(或者DTD)映射爲java對象(.java文件),然後使用這些java對象來解析xml文件。需要使用之前生成java文件,因而要有固定的schema,無法處理動態的xml文件。
首先使用xjc命令,生成java文件
xjc [-options ...]
(生成的文件較多)
JAXBContext jc = JAXBContext.newInstance(“packageName");
Unmarshaller unmarshaller = jc.createUnmarshaller();
Collection collection= (Collection)unmarshaller.unmarshal(new File( "books.xml"));
CollectionType.BooksType booksType =collection.getBooks();
List bookList = booksType.getBook();
for( … ){
test.jaxb.BookType book =(test.jaxb.BookType) bookList.get(i);
System.out.println("Book Name: " + book.getName().trim());
System.out.println("Book ISBN: " + book.getISBN());
}
補充另一種方法:
據悉dom4j在xml解析方面是性能最好的,hibernate等框架都使用它作爲解析的工具。
要使用dom4j讀寫XML文檔,需要先下載dom4j包,dom4j官方網站在 http://www.dom4j.org/
目前最新dom4j包下載地址:http://nchc.dl.sourceforge.net/sourceforge/dom4j/dom4j-1.6.1.zip
解開後有兩個包,僅操作XML文檔的話把dom4j-1.6.1.jar加入工程就可以了,如果需要使用XPath的話還需要加入包jaxen-1.1-beta-7.jar
寫了簡單的dom4j的使用的demo,以備回憶,有些是dom4j的文擋裏例子改編的
使用dom4j解析下面的xml文件。
<?xml version="1.0" encoding="GB2312"?> <?xml-stylesheet type="text/xsl" href="students.xsl"?> <students> <student sn="01"> <name>張三</name> <age>18</age> </student> <student sn="02"> <name>李四</name> <age>20</age> </student> </students>
Parse.java
import java.io.File;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.ProcessingInstruction;
import org.dom4j.VisitorSupport;
import org.dom4j.io.SAXReader;
public class Parse {
public static void main(String[] args) {
SAXReader reader = new SAXReader();
File file = new File("src/students.xml");
try {
Document doc = reader.read(file);
doc.accept(new MyVistor());
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static class MyVistor extends VisitorSupport {
public void visit(Attribute node) {
System.out.println("Attibute:---" + node.getName() + "="+ node.getValue());
}
public void visit(Element node) {
if (node.isTextOnly()) {
System.out.println("Element:---" + node.getName() + "="
+ node.getText());
}else{
System.out.println("--------" + node.getName() + "-------");
}
}
@Override
public void visit(ProcessingInstruction node) {
System.out.println("PI:"+node.getTarget()+" "+node.getText());
}
}
}
使用dom4j來將屬性寫入xml
import java.io.FileWriter;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class DWriter {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
XMLWriter writer = new XMLWriter(new FileWriter("src/author.xml"));
Document doc = createDoc();
writer.write(doc);
writer.close();
// Pretty print the document to System.out
// 設置了打印的格式,將讀出到控制檯的格式進行美化
OutputFormat format = OutputFormat.createPrettyPrint();
writer = new XMLWriter(System.out, format);
writer.write(doc);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Document createDoc() {
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("root");
Element author1 = root.addElement("author").addAttribute("name",
"Kree").addAttribute("location", "UK")
.addText("Kree Strachan");
Element author2 = root.addElement("author").addAttribute("name", "King")
.addAttribute("location", "US").addText("King McWrirter");
return doc;
}
}
使用dom4j寫入到author.xml文件的內容
<?xml version="1.0" encoding="UTF-8"?>
<root>
<author name="Kree" location="UK">Kree Strachan</author>
<author name="King" location="US">King McWrirter</author>
</root>