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

对于读取得方法其实是很简单的,写入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);
  1. DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
  2. DocumentBuilderbuilder=null;
  3. try{
  4. builder=factory.newDocumentBuilder();
  5. }catch(ParserConfigurationExceptionpce){
  6. System.err.println(pce);
  7. System.exit(1);
  8. }
  9. Documentdoc=null;
  10. doc=builder.newDocument();
  11. //下面是建立XML文档内容的过程,
  12. //先建立根元素"学生花名册"
  13. Elementroot=doc.createElement("学生花名册");
  14. //根元素添加上文档
  15. doc.appendChild(root);
  16. //建立"学生"元素,添加到根元素
  17. Elementstudent=doc.createElement("学生");
  18. student.setAttribute("性别",studentBean.getSex());
  19. root.appendChild(student);
  20. //建立"姓名"元素,添加到学生下面,下同
  21. Elementname=doc.createElement("姓名");
  22. student.appendChild(name);
  23. TexttName=doc.createTextNode(studentBean.getName());
  24. name.appendChild(tName);
  25. Elementage=doc.createElement("年龄");
  26. student.appendChild(age);
  27. TexttAge=doc.createTextNode(String.valueOf(studentBean.getAge()));
  28. 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 MyContentHandlerimplements 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. }
  1. importorg.xml.sax.*;
  2. publicclassMyContentHandlerimplementsContentHandler{
  3. ……
  4. }
  5. /**
  6. *当其他某一个调用事件发生时,先调用此方法来在文档中定位。
  7. *@paramlocator
  8. */
  9. publicvoidsetDocumentLocator(Locatorlocator){
  10. }
  11. /**
  12. *在解析整个文档开始时调用
  13. *@throwsSAXException
  14. */
  15. publicvoidstartDocument()throwsSAXException{
  16. System.out.println("**Studentinformationstart**");
  17. }
  18. /**
  19. *在解析整个文档结束时调用
  20. *@throwsSAXException
  21. */
  22. publicvoidendDocument()throwsSAXException{
  23. System.out.println("****Studentinformationend****");
  24. }
  25. /**
  26. *在解析名字空间开始时调用
  27. *@paramprefix
  28. *@paramuri
  29. *@throwsSAXException
  30. */
  31. publicvoidstartPrefixMapping(Stringprefix
  32. ,Stringuri)throwsSAXException{
  33. }
  34. /**
  35. *在解析名字空间结束时调用
  36. *@paramprefix
  37. *@throwsSAXException
  38. */
  39. publicvoidendPrefixMapping(Stringprefix)throwsSAXException{
  40. }
  41. /**
  42. *在解析元素开始时调用
  43. *@paramnamespaceURI
  44. *@paramlocalName
  45. *@paramqName
  46. *@paramatts
  47. *@throwsSAXException
  48. */
  49. publicvoidstartElement(StringnamespaceURI,StringlocalName
  50. ,StringqName,Attributesatts)throwsSAXException{
  51. }
  52. /**在解析元素结束时调用
  53. *@paramnamespaceURI
  54. *@paramlocalName本地名,如student
  55. *@paramqName原始名,如LIT:student
  56. *@throwsSAXException*/
  57. publicvoidendElement(StringnamespaceURI,StringlocalName,StringqName)throwsSAXException{
  58. if(localName.equals(“student”)){
  59. System.out.println(localName+":"+currentData);
  60. }
  61. }

取得元素数据的方法——characters

取得元素数据中的空白的方法——ignorableWhitespace
在解析到处理指令时调用的方法——processingInstruction
当未验证解析器忽略实体时调用的方法——skippedEntity
运行时,只需要使用下列代码:

Java代码 复制代码 收藏代码
  1. MySAXParser mySAXParser = new MySAXParser();
  2. mySAXParser.parserXMLFile("SutInfo.xml");
  1. MySAXParsermySAXParser=newMySAXParser();
  2. 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"));
  1. importorg.jdom.*;
  2. importorg.jdom.input.*;
  3. importorg.jdom.output.*;
  4. SAXBuilderbuilder=newSAXBuilder(false);
  5. //得到Document
  6. Documentdoc=builder.build(fileURI);
  7. //名字空间
  8. Namespacens=Namespace.getNamespace("LIT","http://www.lit.edu.cn/student/");
  9. //取得所有LIT:student节点的集合
  10. ListlstStudents=elmtStuInfo.getChildren("student",ns);
  11. for(…){
  12. ElementelmtStudent=(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(newElement("master",ns).addContent(newEntity("masterName")));
  21. //输出文档
  22. //第一个参数是缩进字符串,这里是4个空格。
  23. //第二个参数是true,表示需要换行。
  24. XMLOutputterprintDoc=newXMLOutputter("",true);
  25. printDoc.output(doc,newFileOutputStream("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. }
  1. JAXBContextjc=JAXBContext.newInstance(“packageName");
  2. Unmarshallerunmarshaller=jc.createUnmarshaller();
  3. Collectioncollection=(Collection)unmarshaller.unmarshal(newFile("books.xml"));
  4. CollectionType.BooksTypebooksType=collection.getBooks();
  5. ListbookList=booksType.getBook();
  6. for(…){
  7. test.jaxb.BookTypebook=(test.jaxb.BookType)bookList.get(i);
  8. System.out.println("BookName:"+book.getName().trim());
  9. System.out.println("BookISBN:"+book.getISBN());
  10. }

补充另一种方法:

据悉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. <?xml-stylesheettype="text/xsl"href="students.xsl"?>
  3. <students>
  4. <studentsn="01">
  5. <name>张三</name>
  6. <age>18</age>
  7. </student>
  8. <studentsn="02">
  9. <name>李四</name>
  10. <age>20</age>
  11. </student>
  12. </students>
  1. <?xmlversion="1.0"encoding="GB2312"?>
  2. <?xml-stylesheettype="text/xsl"href="students.xsl"?>
  3. <students>
  4. <studentsn="01">
  5. <name>张三</name>
  6. <age>18</age>
  7. </student>
  8. <studentsn="02">
  9. <name>李四</name>
  10. <age>20</age>
  11. </student>
  12. </students>

Parse.java

Java代码 复制代码 收藏代码
  1. import java.io.File;
  2. import org.dom4j.Attribute;
  3. import org.dom4j.Document;
  4. import org.dom4j.DocumentException;
  5. import org.dom4j.Element;
  6. import org.dom4j.ProcessingInstruction;
  7. import org.dom4j.VisitorSupport;
  8. import org.dom4j.io.SAXReader;
  9. public class Parse {
  10. public staticvoid main(String[] args) {
  11. SAXReader reader = new SAXReader();
  12. File file = new File("src/students.xml");
  13. try {
  14. Document doc = reader.read(file);
  15. doc.accept(new MyVistor());
  16. } catch (DocumentException e) {
  17. // TODO Auto-generated catch block
  18. e.printStackTrace();
  19. }
  20. }
  21. public staticclass MyVistor extends VisitorSupport {
  22. public void visit(Attribute node) {
  23. System.out.println("Attibute:---" + node.getName() +"="+ node.getValue());
  24. }
  25. public void visit(Element node) {
  26. if (node.isTextOnly()) {
  27. System.out.println("Element:---" + node.getName() +"="
  28. + node.getText());
  29. }else{
  30. System.out.println("--------" + node.getName() +"-------");
  31. }
  32. }
  33. @Override
  34. public void visit(ProcessingInstruction node) {
  35. System.out.println("PI:"+node.getTarget()+" "+node.getText());
  36. }
  37. }
  38. }
  1. importjava.io.File;
  2. importorg.dom4j.Attribute;
  3. importorg.dom4j.Document;
  4. importorg.dom4j.DocumentException;
  5. importorg.dom4j.Element;
  6. importorg.dom4j.ProcessingInstruction;
  7. importorg.dom4j.VisitorSupport;
  8. importorg.dom4j.io.SAXReader;
  9. publicclassParse{
  10. publicstaticvoidmain(String[]args){
  11. SAXReaderreader=newSAXReader();
  12. Filefile=newFile("src/students.xml");
  13. try{
  14. Documentdoc=reader.read(file);
  15. doc.accept(newMyVistor());
  16. }catch(DocumentExceptione){
  17. //TODOAuto-generatedcatchblock
  18. e.printStackTrace();
  19. }
  20. }
  21. publicstaticclassMyVistorextendsVisitorSupport{
  22. publicvoidvisit(Attributenode){
  23. System.out.println("Attibute:---"+node.getName()+"="+node.getValue());
  24. }
  25. publicvoidvisit(Elementnode){
  26. if(node.isTextOnly()){
  27. System.out.println("Element:---"+node.getName()+"="
  28. +node.getText());
  29. }else{
  30. System.out.println("--------"+node.getName()+"-------");
  31. }
  32. }
  33. @Override
  34. publicvoidvisit(ProcessingInstructionnode){
  35. System.out.println("PI:"+node.getTarget()+""+node.getText());
  36. }
  37. }
  38. }

使用dom4j来将属性写入xml

Java代码 复制代码 收藏代码
  1. import java.io.FileWriter;
  2. import java.io.IOException;
  3. import org.dom4j.Document;
  4. import org.dom4j.DocumentHelper;
  5. import org.dom4j.Element;
  6. import org.dom4j.io.OutputFormat;
  7. import org.dom4j.io.XMLWriter;
  8. public class DWriter {
  9. public staticvoid main(String[] args) {
  10. // TODO Auto-generated method stub
  11. try {
  12. XMLWriter writer = new XMLWriter(new FileWriter("src/author.xml"));
  13. Document doc = createDoc();
  14. writer.write(doc);
  15. writer.close();
  16. // Pretty print the document to System.out
  17. // 设置了打印的格式,将读出到控制台的格式进行美化
  18. OutputFormat format = OutputFormat.createPrettyPrint();
  19. writer = new XMLWriter(System.out, format);
  20. writer.write(doc);
  21. } catch (IOException e) {
  22. // TODO Auto-generated catch block
  23. e.printStackTrace();
  24. }
  25. }
  26. public static Document createDoc() {
  27. Document doc = DocumentHelper.createDocument();
  28. Element root = doc.addElement("root");
  29. Element author1 = root.addElement("author").addAttribute("name",
  30. "Kree").addAttribute("location","UK")
  31. .addText("Kree Strachan");
  32. Element author2 = root.addElement("author").addAttribute("name","King")
  33. .addAttribute("location","US").addText("King McWrirter");
  34. return doc;
  35. }
  36. }
  1. importjava.io.FileWriter;
  2. importjava.io.IOException;
  3. importorg.dom4j.Document;
  4. importorg.dom4j.DocumentHelper;
  5. importorg.dom4j.Element;
  6. importorg.dom4j.io.OutputFormat;
  7. importorg.dom4j.io.XMLWriter;
  8. publicclassDWriter{
  9. publicstaticvoidmain(String[]args){
  10. //TODOAuto-generatedmethodstub
  11. try{
  12. XMLWriterwriter=newXMLWriter(newFileWriter("src/author.xml"));
  13. Documentdoc=createDoc();
  14. writer.write(doc);
  15. writer.close();
  16. //PrettyprintthedocumenttoSystem.out
  17. //设置了打印的格式,将读出到控制台的格式进行美化
  18. OutputFormatformat=OutputFormat.createPrettyPrint();
  19. writer=newXMLWriter(System.out,format);
  20. writer.write(doc);
  21. }catch(IOExceptione){
  22. //TODOAuto-generatedcatchblock
  23. e.printStackTrace();
  24. }
  25. }
  26. publicstaticDocumentcreateDoc(){
  27. Documentdoc=DocumentHelper.createDocument();
  28. Elementroot=doc.addElement("root");
  29. Elementauthor1=root.addElement("author").addAttribute("name",
  30. "Kree").addAttribute("location","UK")
  31. .addText("KreeStrachan");
  32. Elementauthor2=root.addElement("author").addAttribute("name","King")
  33. .addAttribute("location","US").addText("KingMcWrirter");
  34. returndoc;
  35. }
  36. }

使用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>

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

对于读取得方法其实是很简单的,写入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);
  1. DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
  2. DocumentBuilderbuilder=null;
  3. try{
  4. builder=factory.newDocumentBuilder();
  5. }catch(ParserConfigurationExceptionpce){
  6. System.err.println(pce);
  7. System.exit(1);
  8. }
  9. Documentdoc=null;
  10. doc=builder.newDocument();
  11. //下面是建立XML文档内容的过程,
  12. //先建立根元素"学生花名册"
  13. Elementroot=doc.createElement("学生花名册");
  14. //根元素添加上文档
  15. doc.appendChild(root);
  16. //建立"学生"元素,添加到根元素
  17. Elementstudent=doc.createElement("学生");
  18. student.setAttribute("性别",studentBean.getSex());
  19. root.appendChild(student);
  20. //建立"姓名"元素,添加到学生下面,下同
  21. Elementname=doc.createElement("姓名");
  22. student.appendChild(name);
  23. TexttName=doc.createTextNode(studentBean.getName());
  24. name.appendChild(tName);
  25. Elementage=doc.createElement("年龄");
  26. student.appendChild(age);
  27. TexttAge=doc.createTextNode(String.valueOf(studentBean.getAge()));
  28. 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 MyContentHandlerimplements 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. }
  1. importorg.xml.sax.*;
  2. publicclassMyContentHandlerimplementsContentHandler{
  3. ……
  4. }
  5. /**
  6. *当其他某一个调用事件发生时,先调用此方法来在文档中定位。
  7. *@paramlocator
  8. */
  9. publicvoidsetDocumentLocator(Locatorlocator){
  10. }
  11. /**
  12. *在解析整个文档开始时调用
  13. *@throwsSAXException
  14. */
  15. publicvoidstartDocument()throwsSAXException{
  16. System.out.println("**Studentinformationstart**");
  17. }
  18. /**
  19. *在解析整个文档结束时调用
  20. *@throwsSAXException
  21. */
  22. publicvoidendDocument()throwsSAXException{
  23. System.out.println("****Studentinformationend****");
  24. }
  25. /**
  26. *在解析名字空间开始时调用
  27. *@paramprefix
  28. *@paramuri
  29. *@throwsSAXException
  30. */
  31. publicvoidstartPrefixMapping(Stringprefix
  32. ,Stringuri)throwsSAXException{
  33. }
  34. /**
  35. *在解析名字空间结束时调用
  36. *@paramprefix
  37. *@throwsSAXException
  38. */
  39. publicvoidendPrefixMapping(Stringprefix)throwsSAXException{
  40. }
  41. /**
  42. *在解析元素开始时调用
  43. *@paramnamespaceURI
  44. *@paramlocalName
  45. *@paramqName
  46. *@paramatts
  47. *@throwsSAXException
  48. */
  49. publicvoidstartElement(StringnamespaceURI,StringlocalName
  50. ,StringqName,Attributesatts)throwsSAXException{
  51. }
  52. /**在解析元素结束时调用
  53. *@paramnamespaceURI
  54. *@paramlocalName本地名,如student
  55. *@paramqName原始名,如LIT:student
  56. *@throwsSAXException*/
  57. publicvoidendElement(StringnamespaceURI,StringlocalName,StringqName)throwsSAXException{
  58. if(localName.equals(“student”)){
  59. System.out.println(localName+":"+currentData);
  60. }
  61. }

取得元素数据的方法——characters

取得元素数据中的空白的方法——ignorableWhitespace
在解析到处理指令时调用的方法——processingInstruction
当未验证解析器忽略实体时调用的方法——skippedEntity
运行时,只需要使用下列代码:

Java代码 复制代码 收藏代码
  1. MySAXParser mySAXParser = new MySAXParser();
  2. mySAXParser.parserXMLFile("SutInfo.xml");
  1. MySAXParsermySAXParser=newMySAXParser();
  2. 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"));
  1. importorg.jdom.*;
  2. importorg.jdom.input.*;
  3. importorg.jdom.output.*;
  4. SAXBuilderbuilder=newSAXBuilder(false);
  5. //得到Document
  6. Documentdoc=builder.build(fileURI);
  7. //名字空间
  8. Namespacens=Namespace.getNamespace("LIT","http://www.lit.edu.cn/student/");
  9. //取得所有LIT:student节点的集合
  10. ListlstStudents=elmtStuInfo.getChildren("student",ns);
  11. for(…){
  12. ElementelmtStudent=(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(newElement("master",ns).addContent(newEntity("masterName")));
  21. //输出文档
  22. //第一个参数是缩进字符串,这里是4个空格。
  23. //第二个参数是true,表示需要换行。
  24. XMLOutputterprintDoc=newXMLOutputter("",true);
  25. printDoc.output(doc,newFileOutputStream("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. }
  1. JAXBContextjc=JAXBContext.newInstance(“packageName");
  2. Unmarshallerunmarshaller=jc.createUnmarshaller();
  3. Collectioncollection=(Collection)unmarshaller.unmarshal(newFile("books.xml"));
  4. CollectionType.BooksTypebooksType=collection.getBooks();
  5. ListbookList=booksType.getBook();
  6. for(…){
  7. test.jaxb.BookTypebook=(test.jaxb.BookType)bookList.get(i);
  8. System.out.println("BookName:"+book.getName().trim());
  9. System.out.println("BookISBN:"+book.getISBN());
  10. }

补充另一种方法:

据悉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. <?xml-stylesheettype="text/xsl"href="students.xsl"?>
  3. <students>
  4. <studentsn="01">
  5. <name>张三</name>
  6. <age>18</age>
  7. </student>
  8. <studentsn="02">
  9. <name>李四</name>
  10. <age>20</age>
  11. </student>
  12. </students>
  1. <?xmlversion="1.0"encoding="GB2312"?>
  2. <?xml-stylesheettype="text/xsl"href="students.xsl"?>
  3. <students>
  4. <studentsn="01">
  5. <name>张三</name>
  6. <age>18</age>
  7. </student>
  8. <studentsn="02">
  9. <name>李四</name>
  10. <age>20</age>
  11. </student>
  12. </students>

Parse.java

Java代码 复制代码 收藏代码
  1. import java.io.File;
  2. import org.dom4j.Attribute;
  3. import org.dom4j.Document;
  4. import org.dom4j.DocumentException;
  5. import org.dom4j.Element;
  6. import org.dom4j.ProcessingInstruction;
  7. import org.dom4j.VisitorSupport;
  8. import org.dom4j.io.SAXReader;
  9. public class Parse {
  10. public staticvoid main(String[] args) {
  11. SAXReader reader = new SAXReader();
  12. File file = new File("src/students.xml");
  13. try {
  14. Document doc = reader.read(file);
  15. doc.accept(new MyVistor());
  16. } catch (DocumentException e) {
  17. // TODO Auto-generated catch block
  18. e.printStackTrace();
  19. }
  20. }
  21. public staticclass MyVistor extends VisitorSupport {
  22. public void visit(Attribute node) {
  23. System.out.println("Attibute:---" + node.getName() +"="+ node.getValue());
  24. }
  25. public void visit(Element node) {
  26. if (node.isTextOnly()) {
  27. System.out.println("Element:---" + node.getName() +"="
  28. + node.getText());
  29. }else{
  30. System.out.println("--------" + node.getName() +"-------");
  31. }
  32. }
  33. @Override
  34. public void visit(ProcessingInstruction node) {
  35. System.out.println("PI:"+node.getTarget()+" "+node.getText());
  36. }
  37. }
  38. }
  1. importjava.io.File;
  2. importorg.dom4j.Attribute;
  3. importorg.dom4j.Document;
  4. importorg.dom4j.DocumentException;
  5. importorg.dom4j.Element;
  6. importorg.dom4j.ProcessingInstruction;
  7. importorg.dom4j.VisitorSupport;
  8. importorg.dom4j.io.SAXReader;
  9. publicclassParse{
  10. publicstaticvoidmain(String[]args){
  11. SAXReaderreader=newSAXReader();
  12. Filefile=newFile("src/students.xml");
  13. try{
  14. Documentdoc=reader.read(file);
  15. doc.accept(newMyVistor());
  16. }catch(DocumentExceptione){
  17. //TODOAuto-generatedcatchblock
  18. e.printStackTrace();
  19. }
  20. }
  21. publicstaticclassMyVistorextendsVisitorSupport{
  22. publicvoidvisit(Attributenode){
  23. System.out.println("Attibute:---"+node.getName()+"="+node.getValue());
  24. }
  25. publicvoidvisit(Elementnode){
  26. if(node.isTextOnly()){
  27. System.out.println("Element:---"+node.getName()+"="
  28. +node.getText());
  29. }else{
  30. System.out.println("--------"+node.getName()+"-------");
  31. }
  32. }
  33. @Override
  34. publicvoidvisit(ProcessingInstructionnode){
  35. System.out.println("PI:"+node.getTarget()+""+node.getText());
  36. }
  37. }
  38. }

使用dom4j来将属性写入xml

Java代码 复制代码 收藏代码
  1. import java.io.FileWriter;
  2. import java.io.IOException;
  3. import org.dom4j.Document;
  4. import org.dom4j.DocumentHelper;
  5. import org.dom4j.Element;
  6. import org.dom4j.io.OutputFormat;
  7. import org.dom4j.io.XMLWriter;
  8. public class DWriter {
  9. public staticvoid main(String[] args) {
  10. // TODO Auto-generated method stub
  11. try {
  12. XMLWriter writer = new XMLWriter(new FileWriter("src/author.xml"));
  13. Document doc = createDoc();
  14. writer.write(doc);
  15. writer.close();
  16. // Pretty print the document to System.out
  17. // 设置了打印的格式,将读出到控制台的格式进行美化
  18. OutputFormat format = OutputFormat.createPrettyPrint();
  19. writer = new XMLWriter(System.out, format);
  20. writer.write(doc);
  21. } catch (IOException e) {
  22. // TODO Auto-generated catch block
  23. e.printStackTrace();
  24. }
  25. }
  26. public static Document createDoc() {
  27. Document doc = DocumentHelper.createDocument();
  28. Element root = doc.addElement("root");
  29. Element author1 = root.addElement("author").addAttribute("name",
  30. "Kree").addAttribute("location","UK")
  31. .addText("Kree Strachan");
  32. Element author2 = root.addElement("author").addAttribute("name","King")
  33. .addAttribute("location","US").addText("King McWrirter");
  34. return doc;
  35. }
  36. }
  1. importjava.io.FileWriter;
  2. importjava.io.IOException;
  3. importorg.dom4j.Document;
  4. importorg.dom4j.DocumentHelper;
  5. importorg.dom4j.Element;
  6. importorg.dom4j.io.OutputFormat;
  7. importorg.dom4j.io.XMLWriter;
  8. publicclassDWriter{
  9. publicstaticvoidmain(String[]args){
  10. //TODOAuto-generatedmethodstub
  11. try{
  12. XMLWriterwriter=newXMLWriter(newFileWriter("src/author.xml"));
  13. Documentdoc=createDoc();
  14. writer.write(doc);
  15. writer.close();
  16. //PrettyprintthedocumenttoSystem.out
  17. //设置了打印的格式,将读出到控制台的格式进行美化
  18. OutputFormatformat=OutputFormat.createPrettyPrint();
  19. writer=newXMLWriter(System.out,format);
  20. writer.write(doc);
  21. }catch(IOExceptione){
  22. //TODOAuto-generatedcatchblock
  23. e.printStackTrace();
  24. }
  25. }
  26. publicstaticDocumentcreateDoc(){
  27. Documentdoc=DocumentHelper.createDocument();
  28. Elementroot=doc.addElement("root");
  29. Elementauthor1=root.addElement("author").addAttribute("name",
  30. "Kree").addAttribute("location","UK")
  31. .addText("KreeStrachan");
  32. Elementauthor2=root.addElement("author").addAttribute("name","King")
  33. .addAttribute("location","US").addText("KingMcWrirter");
  34. returndoc;
  35. }
  36. }

使用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>

你可能感兴趣的:(解析xml)