java解析xml文件

在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. //获得根节点StuInfo 
  30. Element elmtStuInfo = doc.getDocumentElement(); 
  31. //得到所有student节点 
  32. NodeList nlStudent = elmtStuInfo.getElementsByTagNameNS( 
  33.                                        strNamespace, "student"); 
  34. for (……){ 
  35.      //当前student节点元素 
  36.      Element elmtStudent = (Element)nlStudent.item(i); 
  37.      NodeList nlCurrent =              elmtStudent.getElementsByTagNameNS( 
  38.                                      strNamespace, "name"); 
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. Document doc = null;  
  9. doc = builder .newDocument(); 
  10. //下面是建立XML文档内容的过程, 
  11. //先建立根元素"学生花名册"  
  12. Element root = doc.createElement("学生花名册");  
  13. //根元素添加上文档  
  14. doc.appendChild(root);  
  15. //建立"学生"元素,添加到根元素  
  16. Element student = doc.createElement("学生");  
  17. student.setAttribute("性别", studentBean.getSex());  
  18. root.appendChild(student);  
  19. //建立"姓名"元素,添加到学生下面,下同  
  20. Element name = doc.createElement("姓名");  
  21. student.appendChild(name);  
  22. Text tName = doc.createTextNode(studentBean.getName());  
  23. name.appendChild(tName); 
  24. Element age = doc.createElement("年龄");  
  25. student.appendChild(age);  
  26. Text tAge = doc.createTextNode(String.valueOf(studentBean.getAge()));  
  27. 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.      * @param locator
  7.      */ 
  8.     public void setDocumentLocator(Locator locator){ 
  9.     } 
  10. /**
  11.      * 在解析整个文档开始时调用
  12.      * @throws SAXException
  13.      */ 
  14.     public void startDocument() throws SAXException{ 
  15.         System.out.println("** Student information start **"); 
  16.     } 
  17. /**
  18.      * 在解析整个文档结束时调用
  19.      * @throws SAXException
  20.      */ 
  21.     public void endDocument() throws SAXException{ 
  22.         System.out.println("**** Student information end ****"); 
  23.     } 
  24. /**
  25.      * 在解析名字空间开始时调用
  26.      * @param prefix
  27.      * @param uri
  28.      * @throws SAXException
  29.      */ 
  30.     public void startPrefixMapping(String prefix 
  31.         , String uri) throws SAXException{ 
  32.     } 
  33. /**
  34.      * 在解析名字空间结束时调用
  35.      * @param prefix
  36.      * @throws SAXException
  37.      */ 
  38.     public void endPrefixMapping(String prefix) throws SAXException{ 
  39.     } 
  40. /**
  41.      * 在解析元素开始时调用
  42.      * @param namespaceURI
  43.      * @param localName
  44.      * @param qName
  45.      * @param atts
  46.      * @throws SAXException
  47.      */ 
  48.     public void startElement(String namespaceURI, String localName 
  49.         , String qName, Attributes atts) throws SAXException{ 
  50.     } 
  51. /** 在解析元素结束时调用
  52.      * @param namespaceURI
  53.      * @param localName 本地名,如student
  54.      * @param qName 原始名,如LIT:student
  55.      * @throws SAXException   */ 
  56.     public void endElement(String namespaceURI, String localName,String qName) throws SAXException{ 
  57.   if (localName.equals(“student”)){ 
  58.             System.out.println(localName+":"+currentData); 
  59.         } 
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. elmtLesson.getChild("lessonScore" , ns).setText("100"); 
  16. //删除 
  17. elmtStuInfo.removeChild("master", ns); 
  18. //添加 
  19. elmtStuInfo.addContent(new Element("master" , ns).addContent(new Entity("masterName"))); 
  20. //输出文档 
  21. //第一个参数是缩进字符串,这里是4个空格。 
  22. //第二个参数是true,表示需要换行。 
  23. XMLOutputter printDoc = new XMLOutputter(" ", true); 
  24. 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()); 
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.     } 
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.     } 
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> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值