java解析xml文件

转自http://blog.csdn.net/lg831229/article/details/9100095,感觉写的很好啊。

在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"); 
[java]  view plain copy
  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. }  

对于读取得方法其实是很简单的,写入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); 
[java]  view plain copy
  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);  

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.         } 
[java]  view plain copy
  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. }  

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

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

Java代码  复制代码  收藏代码
  1. MySAXParser mySAXParser = new MySAXParser(); 
  2. mySAXParser.parserXMLFile("SutInfo.xml"); 
[java]  view plain copy
  1. MySAXParser mySAXParser = new MySAXParser();  
  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. 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")); 
[java]  view plain copy
  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"));  

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()); 
[java]  view plain copy
  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. }  

补充另一种方法:

据悉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]  view plain copy
  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>  

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.     } 
[java]  view plain copy
  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. }  

使用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.     } 
[java]  view plain copy
  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. }  

使用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文件)