`

在java环境下读取xml文件的方法主要有4种:DOM、SAX、JDOM、JAXB

阅读更多

在java环境下读取xml文件的方法主要有4种:DOM、SAX、JDOM、JAXB


1.  DOM(Document Object Model) 


 此 方法主要由W3C提供,它将xml文件全部读入内存中,然后将各个元素组成一棵数据树,以便快速的访问各个节点 。 因此非常消耗系统性能 ,对比较大的文档不适宜采用DOM方法来解析。 DOM API 直接沿袭了 XML 规范。每个结点都可以扩展的基于 Node 的接口,就多态性的观点来讲,它是优秀的,但是在 Java 语言中的应用不方便,并且可读性不强。
 实例:

 

Java代码 复制代码
  1. import javax.xml.parsers.*;    
  2. //XML解析器接口    
  3. import org.w3c.dom.*;    
  4. //XML的DOM实现    
  5. import org.apache.crimson.tree.XmlDocument;   
  6. //写XML文件要用到   
  7. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();   
  8.  //允许名字空间   
  9.  factory.setNamespaceAware(true);   
  10.  //允许验证   
  11.  factory.setValidating(true);   
  12.  //获得DocumentBuilder的一个实例   
  13. try {    
  14.  DocumentBuilder builder = factory.newDocumentBuilder();   
  15. catch (ParserConfigurationException pce) {    
  16. System.err.println(pce);   
  17. //  出异常时输出异常信息,然后退出,下同   
  18. System.exit(1);   
  19. }    
  20. //解析文档,并获得一个Document实例。    
  21. try {    
  22. Document doc = builder.parse(fileURI);   
  23. catch (DOMException dom) {   
  24. System.err.println(dom.getMessage());   
  25. System.exit(1);   
  26. catch (IOException ioe) {   
  27. System.err.println(ioe);   
  28. System.exit(1);        
  29. }   
  30. //获得根节点StuInfo   
  31. Element elmtStuInfo = doc.getDocumentElement();   
  32. //得到所有student节点   
  33.  NodeList nlStudent = elmtStuInfo.getElementsByTagNameNS(   
  34.                                        strNamespace, "student");   
  35. for (……){   
  36.      //当前student节点元素   
  37.      Element elmtStudent = (Element)nlStudent.item(i);   
  38.      NodeList nlCurrent =              elmtStudent.getElementsByTagNameNS(   
  39.                                      strNamespace, "name");   
  40. }  
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文件也是一样不复杂。

 

 

Java代码 复制代码
  1. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();    
  2. DocumentBuilder builder = null;    
  3. try {    
  4. builder = factory .newDocumentBuilder();    
  5. catch (ParserConfigurationException pce) {    
  6. System.err.println(pce);    
  7. System.exit(1);    
  8. }   
  9. Document doc = null;    
  10. doc = builder .newDocument();   
  11. //下面是建立XML文档内容的过程,   
  12. //先建立根元素"学生花名册"    
  13. Element root = doc.createElement("学生花名册");    
  14. //根元素添加上文档    
  15. doc.appendChild(root);    
  16. //建立"学生"元素,添加到根元素    
  17. Element student = doc.createElement("学生");    
  18. student.setAttribute("性别", studentBean.getSex());    
  19. root.appendChild(student);    
  20. //建立"姓名"元素,添加到学生下面,下同    
  21. Element name = doc.createElement("姓名");    
  22. student.appendChild(name);    
  23. Text tName = doc.createTextNode(studentBean.getName());    
  24. name.appendChild(tName);   
  25. Element age = doc.createElement("年龄");    
  26. student.appendChild(age);    
  27. Text tAge = doc.createTextNode(String.valueOf(studentBean.getAge()));    
  28. age.appendChild(tAge);  
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 )。

 

Java代码 复制代码
  1. import org.xml.sax.*;   
  2. public  class  MyContentHandler implements ContentHandler {   
  3.   … …   
  4. }   
  5. /**  
  6.      * 当其他某一个调用事件发生时,先调用此方法来在文档中定位。  
  7.      * @param locator  
  8.      */  
  9.     public void setDocumentLocator(Locator locator){   
  10.     }   
  11. /**  
  12.      * 在解析整个文档开始时调用  
  13.      * @throws SAXException  
  14.      */  
  15.     public void startDocument() throws SAXException{   
  16.         System.out.println("** Student information start **");   
  17.     }   
  18. /**  
  19.      * 在解析整个文档结束时调用  
  20.      * @throws SAXException  
  21.      */  
  22.     public void endDocument() throws SAXException{   
  23.         System.out.println("**** Student information end ****");   
  24.     }   
  25. /**  
  26.      * 在解析名字空间开始时调用  
  27.      * @param prefix  
  28.      * @param uri  
  29.      * @throws SAXException  
  30.      */  
  31.     public void startPrefixMapping(String prefix   
  32.         , String uri) throws SAXException{   
  33.     }   
  34. /**  
  35.      * 在解析名字空间结束时调用  
  36.      * @param prefix  
  37.      * @throws SAXException  
  38.      */  
  39.     public void endPrefixMapping(String prefix) throws SAXException{   
  40.     }   
  41. /**  
  42.      * 在解析元素开始时调用  
  43.      * @param namespaceURI  
  44.      * @param localName  
  45.      * @param qName  
  46.      * @param atts  
  47.      * @throws SAXException  
  48.      */  
  49.     public void startElement(String namespaceURI, String localName   
  50.         , String qName, Attributes atts) throws SAXException{   
  51.     }   
  52. /** 在解析元素结束时调用  
  53.      * @param namespaceURI  
  54.      * @param localName 本地名,如student  
  55.      * @param qName 原始名,如LIT:student  
  56.      * @throws SAXException   */  
  57.     public void endElement(String namespaceURI, String localName,String qName) throws SAXException{   
  58.   if (localName.equals(“student”)){   
  59.             System.out.println(localName+":"+currentData);   
  60.         }   
  61. }  
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
运行时,只需要使用下列代码:

 

Java代码 复制代码
  1. MySAXParser mySAXParser = new MySAXParser();   
  2. mySAXParser.parserXMLFile("SutInfo.xml");  
MySAXParser mySAXParser = new MySAXParser();
mySAXParser.parserXMLFile("SutInfo.xml");

 3.JDOM

 

JDOM的处理方式有些类似于DOM,但它主要是用SAX实现的 。JDOM用Java的数据类型来定义操作数据树的各个节点 。JDOM的性能也很优越。

 

Java代码 复制代码
  1. import org.jdom.*;   
  2. import org.jdom.input.*;   
  3. import org.jdom.output.*;   
  4. SAXBuilder builder = new SAXBuilder(false);   
  5. //得到Document   
  6. Document doc = builder.build(fileURI);   
  7. //名字空间   
  8. Namespace ns = Namespace.getNamespace("LIT" , "http://www.lit.edu.cn/student/ ");   
  9. //取得所有LIT:student节点的集合   
  10. List lstStudents = elmtStuInfo.getChildren("student",ns);   
  11. for ( … ){   
  12.  Element elmtStudent = (Element)lstStudents.get(i);   
  13.  elmtStudent.getChildTextTrim("name", ns);   
  14. }   
  15. //修改   
  16. elmtLesson.getChild("lessonScore" , ns).setText("100");   
  17. //删除   
  18. elmtStuInfo.removeChild("master", ns);   
  19. //添加   
  20. elmtStuInfo.addContent(new Element("master" , ns).addContent(new Entity("masterName")));   
  21. //输出文档   
  22. //第一个参数是缩进字符串,这里是4个空格。   
  23. //第二个参数是true,表示需要换行。   
  24. XMLOutputter printDoc = new XMLOutputter(" "true);   
  25.  printDoc.output(doc, new FileOutputStream("StuInfo.xml"));  
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 ...]

(生成的文件较多)

Java代码 复制代码
  1. JAXBContext jc = JAXBContext.newInstance(“packageName");   
  2.  Unmarshaller unmarshaller = jc.createUnmarshaller();   
  3. Collection collection= (Collection)unmarshaller.unmarshal(new File( "books.xml"));   
  4. CollectionType.BooksType booksType =collection.getBooks();   
  5. List bookList = booksType.getBook();   
  6. for( … ){   
  7.  test.jaxb.BookType book =(test.jaxb.BookType) bookList.get(i);   
  8.  System.out.println("Book Name: " + book.getName().trim());   
  9.    System.out.println("Book ISBN: " +  book.getISBN());   
  10. }  
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代码 复制代码
  1. <?xml version="1.0" encoding="GB2312"?>    
  2.   
  3. <?xml-stylesheet type="text/xsl" href="students.xsl"?>  
  4.   
  5. <students>  
  6.     <student sn="01">  
  7.         <name>张三</name>  
  8.         <age>18</age>  
  9.     </student>  
  10.        
  11.     <student sn="02">  
  12.         <name>李四</name>  
  13.         <age>20</age>  
  14.     </student>  
  15. </students>  
<?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 

 

Java代码 复制代码
  1. import java.io.File;   
  2.   
  3. import org.dom4j.Attribute;   
  4. import org.dom4j.Document;   
  5. import org.dom4j.DocumentException;   
  6. import org.dom4j.Element;   
  7. import org.dom4j.ProcessingInstruction;   
  8. import org.dom4j.VisitorSupport;   
  9. import org.dom4j.io.SAXReader;   
  10.   
  11. public class Parse {   
  12.   
  13.     public static void main(String[] args) {   
  14.         SAXReader reader = new SAXReader();   
  15.         File file = new File("src/students.xml");   
  16.         try {   
  17.             Document doc = reader.read(file);   
  18.             doc.accept(new MyVistor());   
  19.         } catch (DocumentException e) {   
  20.             // TODO Auto-generated catch block   
  21.             e.printStackTrace();   
  22.         }   
  23.     }   
  24.   
  25.     public static class MyVistor extends VisitorSupport {   
  26.         public void visit(Attribute node) {   
  27.             System.out.println("Attibute:---" + node.getName() + "="+ node.getValue());   
  28.         }   
  29.   
  30.         public void visit(Element node) {   
  31.             if (node.isTextOnly()) {   
  32.                 System.out.println("Element:---" + node.getName() + "="  
  33.                         + node.getText());   
  34.             }else{   
  35.                 System.out.println("--------" + node.getName() + "-------");   
  36.             }   
  37.         }   
  38.   
  39.         @Override  
  40.         public void visit(ProcessingInstruction node) {   
  41.             System.out.println("PI:"+node.getTarget()+" "+node.getText());   
  42.         }   
  43.     }   
  44. }  
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 

 

Java代码 复制代码
  1. import java.io.FileWriter;   
  2. import java.io.IOException;   
  3.   
  4. import org.dom4j.Document;   
  5. import org.dom4j.DocumentHelper;   
  6. import org.dom4j.Element;   
  7. import org.dom4j.io.OutputFormat;   
  8. import org.dom4j.io.XMLWriter;   
  9.   
  10. public class DWriter {   
  11.   
  12.     public static void main(String[] args) {   
  13.         // TODO Auto-generated method stub   
  14.         try {   
  15.             XMLWriter writer = new XMLWriter(new FileWriter("src/author.xml"));   
  16.             Document doc = createDoc();   
  17.             writer.write(doc);   
  18.             writer.close();   
  19.   
  20.             // Pretty print the document to System.out   
  21.             // 设置了打印的格式,将读出到控制台的格式进行美化   
  22.             OutputFormat format = OutputFormat.createPrettyPrint();   
  23.             writer = new XMLWriter(System.out, format);   
  24.             writer.write(doc);   
  25.   
  26.         } catch (IOException e) {   
  27.             // TODO Auto-generated catch block   
  28.             e.printStackTrace();   
  29.         }   
  30.     }   
  31.   
  32.     public static Document createDoc() {   
  33.         Document doc = DocumentHelper.createDocument();   
  34.         Element root = doc.addElement("root");   
  35.         Element author1 = root.addElement("author").addAttribute("name",   
  36.                 "Kree").addAttribute("location""UK")   
  37.                 .addText("Kree Strachan");   
  38.         Element author2 = root.addElement("author").addAttribute("name""King")   
  39.                 .addAttribute("location""US").addText("King McWrirter");   
  40.         return doc;   
  41.     }   
  42. }  
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文件的内容

 

Java代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <root>   
  3. <author name="Kree" location="UK">Kree Strachan</author>   
  4. <author name="King" location="US">King McWrirter</author>   
  5. </root>  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics