java解析xml的四种方法 2

3.DOM4J生成和解析XML文档
    DOM4J 是一个非常非常优秀的Java XML API,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件。如今你可以看到越来越多的 Java 软件都在使用 DOM4J 来读写 XML,特别值得一提的是连 Sun 的 JAXM 也在用 DOM4J。
Java代码 <embed height="15" width="14" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="always" quality="high" flashvars="clipboard=package%20com.alisoft.facepay.framework.bean%3B%0A%0Aimport%20java.io.File%3B%0Aimport%20java.io.FileWriter%3B%0Aimport%20java.io.IOException%3B%0Aimport%20java.io.Writer%3B%0Aimport%20java.util.Iterator%3B%0A%0Aimport%20org.dom4j.Document%3B%0Aimport%20org.dom4j.DocumentException%3B%0Aimport%20org.dom4j.DocumentHelper%3B%0Aimport%20org.dom4j.Element%3B%0Aimport%20org.dom4j.io.SAXReader%3B%0Aimport%20org.dom4j.io.XMLWriter%3B%0A%2F**%0A%20*%20%0A%20*%20%40author%20hongliang.dinghl%0A%20*%20Dom4j%20%E7%94%9F%E6%88%90XML%E6%96%87%E6%A1%A3%E4%B8%8E%E8%A7%A3%E6%9E%90XML%E6%96%87%E6%A1%A3%0A%20*%2F%0Apublic%20class%20Dom4jDemo%20implements%20XmlDocument%20%7B%0A%0A%09public%20void%20createXml(String%20fileName)%20%7B%0A%09%09Document%20document%20%3D%20DocumentHelper.createDocument()%3B%0A%09%09Element%20employees%3Ddocument.addElement(%22employees%22)%3B%0A%09%09Element%20employee%3Demployees.addElement(%22employee%22)%3B%0A%09%09Element%20name%3D%20employee.addElement(%22name%22)%3B%0A%09%09name.setText(%22ddvip%22)%3B%0A%09%09Element%20sex%3Demployee.addElement(%22sex%22)%3B%0A%09%09sex.setText(%22m%22)%3B%0A%09%09Element%20age%3Demployee.addElement(%22age%22)%3B%0A%09%09age.setText(%2229%22)%3B%0A%09%09try%20%7B%0A%09%09%09Writer%20fileWriter%3Dnew%20FileWriter(fileName)%3B%0A%09%09%09XMLWriter%20xmlWriter%3Dnew%20XMLWriter(fileWriter)%3B%0A%09%09%09xmlWriter.write(document)%3B%0A%09%09%09xmlWriter.close()%3B%0A%09%09%7D%20catch%20(IOException%20e)%20%7B%0A%09%09%09%0A%09%09%09System.out.println(e.getMessage())%3B%0A%09%09%7D%0A%09%09%0A%09%09%0A%09%7D%0A%0A%0A%09public%20void%20parserXml(String%20fileName)%20%7B%0A%09%09%20File%20inputXml%3Dnew%20File(fileName)%3B%0A%09%09%20SAXReader%20saxReader%20%3D%20new%20SAXReader()%3B%0A%09%09%20try%20%7B%0A%09%09%09Document%20document%20%3D%20saxReader.read(inputXml)%3B%0A%09%09%09Element%20employees%3Ddocument.getRootElement()%3B%0A%09%09%09for(Iterator%20i%20%3D%20employees.elementIterator()%3B%20i.hasNext()%3B)%7B%0A%09%09%09%09%20Element%20employee%20%3D%20(Element)%20i.next()%3B%0A%09%09%09%09%20for(Iterator%20j%20%3D%20employee.elementIterator()%3B%20j.hasNext()%3B)%7B%0A%09%09%09%09%09%20Element%20node%3D(Element)%20j.next()%3B%0A%09%09%09%09%09%20System.out.println(node.getName()%2B%22%3A%22%2Bnode.getText())%3B%0A%09%09%09%09%20%7D%0A%0A%09%09%09%7D%0A%09%09%7D%20catch%20(DocumentException%20e)%20%7B%0A%09%09%09System.out.println(e.getMessage())%3B%0A%09%09%7D%0A%20%20%20%20%20System.out.println(%22dom4j%20parserXml%22)%3B%0A%09%7D%0A%0A%0A%7D%0A4.JDOM%E7%94%9F%E6%88%90%E5%92%8C%E8%A7%A3%E6%9E%90XML%0A%E4%B8%BA%E5%87%8F%E5%B0%91DOM%E3%80%81SAX%E7%9A%84%E7%BC%96%E7%A0%81%E9%87%8F%EF%BC%8C%E5%87%BA%E7%8E%B0%E4%BA%86JDOM%EF%BC%9B%E4%BC%98%E7%82%B9%EF%BC%9A20-80%E5%8E%9F%E5%88%99%EF%BC%8C%E6%9E%81%E5%A4%A7%E5%87%8F%E5%B0%91%E4%BA%86%E4%BB%A3%E7%A0%81%E9%87%8F%E3%80%82%E4%BD%BF%E7%94%A8%E5%9C%BA%E5%90%88%EF%BC%9A%E8%A6%81%E5%AE%9E%E7%8E%B0%E7%9A%84%E5%8A%9F%E8%83%BD%E7%AE%80%E5%8D%95%EF%BC%8C%E5%A6%82%E8%A7%A3%E6%9E%90%E3%80%81%E5%88%9B%E5%BB%BA%E7%AD%89%EF%BC%8C%E4%BD%86%E5%9C%A8%E5%BA%95%E5%B1%82%EF%BC%8CJDOM%E8%BF%98%E6%98%AF%E4%BD%BF%E7%94%A8SAX%EF%BC%88%E6%9C%80%E5%B8%B8%E7%94%A8%EF%BC%89%E3%80%81DOM%E3%80%81Xanan%E6%96%87%E6%A1%A3%E3%80%82%0A%3Cpre%20name%3D%22code%22%20class%3D%22java%22%3Epackage%20com.alisoft.facepay.framework.bean%3B%0A%0Aimport%20java.io.FileNotFoundException%3B%0Aimport%20java.io.FileOutputStream%3B%0Aimport%20java.io.IOException%3B%0Aimport%20java.util.List%3B%0A%0Aimport%20org.jdom.Document%3B%0Aimport%20org.jdom.Element%3B%0Aimport%20org.jdom.JDOMException%3B%0Aimport%20org.jdom.input.SAXBuilder%3B%0Aimport%20org.jdom.output.XMLOutputter%3B%0A%2F**%0A%20*%20%0A%20*%20%40author%20hongliang.dinghl%0A%20*%20JDOM%20%E7%94%9F%E6%88%90%E4%B8%8E%E8%A7%A3%E6%9E%90XML%E6%96%87%E6%A1%A3%0A%20*%20%0A%20*%2F%0Apublic%20class%20JDomDemo%20implements%20XmlDocument%20%7B%0A%0A%09public%20void%20createXml(String%20fileName)%20%7B%0A%09%20%20Document%20document%3B%0A%09%20%20Element%20%20root%3B%0A%09%20%20root%3Dnew%20Element(%22employees%22)%3B%0A%09%20%20document%3Dnew%20Document(root)%3B%0A%09%20%20Element%20employee%3Dnew%20Element(%22employee%22)%3B%0A%09%20%20root.addContent(employee)%3B%0A%09%20%20Element%20name%3Dnew%20Element(%22name%22)%3B%0A%09%20%20name.setText(%22ddvip%22)%3B%0A%09%20%20employee.addContent(name)%3B%0A%09%20%20Element%20sex%3Dnew%20Element(%22sex%22)%3B%0A%09%20%20sex.setText(%22m%22)%3B%0A%09%20%20employee.addContent(sex)%3B%0A%09%20%20Element%20age%3Dnew%20Element(%22age%22)%3B%0A%09%20%20age.setText(%2223%22)%3B%0A%09%20%20employee.addContent(age)%3B%0A%09%20%20XMLOutputter%20XMLOut%20%3D%20new%20XMLOutputter()%3B%0A%09%20%20try%20%7B%0A%09%09XMLOut.output(document%2C%20new%20FileOutputStream(fileName))%3B%0A%09%7D%20catch%20(FileNotFoundException%20e)%20%7B%0A%09%09e.printStackTrace()%3B%0A%09%7D%20catch%20(IOException%20e)%20%7B%0A%09%09e.printStackTrace()%3B%0A%09%7D%0A%0A%09%7D%0A%0A%09public%20void%20parserXml(String%20fileName)%20%7B%0A%09%09SAXBuilder%20builder%3Dnew%20SAXBuilder(false)%3B%20%0A%09%09try%20%7B%0A%09%09%09Document%20document%3Dbuilder.build(fileName)%3B%0A%09%09%09Element%20employees%3Ddocument.getRootElement()%3B%20%0A%09%09%09List%20employeeList%3Demployees.getChildren(%22employee%22)%3B%0A%09%09%09for(int%20i%3D0%3Bi%3CemployeeList.size()%3Bi%2B%2B)%7B%0A%09%09%09%09Element%20employee%3D(Element)employeeList.get(i)%3B%0A%09%09%09%09List%20employeeInfo%3Demployee.getChildren()%3B%0A%09%09%09%09for(int%20j%3D0%3Bj%3CemployeeInfo.size()%3Bj%2B%2B)%7B%0A%09%09%09%09%09System.out.println(((Element)employeeInfo.get(j)).getName()%2B%22%3A%22%2B((Element)employeeInfo.get(j)).getValue())%3B%0A%09%09%09%09%09%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%20catch%20(JDOMException%20e)%20%7B%0A%09%09%0A%09%09%09e.printStackTrace()%3B%0A%09%09%7D%20catch%20(IOException%20e)%20%7B%0A%09%09%0A%09%09%09e.printStackTrace()%3B%0A%09%09%7D%20%0A%0A%09%7D%0A%7D%0A%20%20%3C%2Fpre%3E" src="http://ddviplinux.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" lk_mediaid="lk_juiceapp_mediaPopup_1238469223836" lk_media="yes">
  1. package com.alisoft.facepay.framework.bean;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileWriter;  
  5. import java.io.IOException;  
  6. import java.io.Writer;  
  7. import java.util.Iterator;  
  8.   
  9. import org.dom4j.Document;  
  10. import org.dom4j.DocumentException;  
  11. import org.dom4j.DocumentHelper;  
  12. import org.dom4j.Element;  
  13. import org.dom4j.io.SAXReader;  
  14. import org.dom4j.io.XMLWriter;  
  15. /**
  16. *
  17. * @author hongliang.dinghl
  18. * Dom4j 生成XML文档与解析XML文档
  19. */  
  20. public class Dom4jDemo implements XmlDocument {  
  21.   
  22.     public void createXml(String fileName) {  
  23.          Document document = DocumentHelper.createDocument();  
  24.          Element employees=document.addElement("employees");  
  25.          Element employee=employees.addElement("employee");  
  26.          Element name= employee.addElement("name");  
  27.          name.setText("ddvip");  
  28.          Element sex=employee.addElement("sex");  
  29.          sex.setText("m");  
  30.          Element age=employee.addElement("age");  
  31.          age.setText("29");  
  32.         try {  
  33.              Writer fileWriter=new FileWriter(fileName);  
  34.              XMLWriter xmlWriter=new XMLWriter(fileWriter);  
  35.              xmlWriter.write(document);  
  36.              xmlWriter.close();  
  37.          } catch (IOException e) {  
  38.               
  39.              System.out.println(e.getMessage());  
  40.          }  
  41.           
  42.           
  43.      }  
  44.   
  45.   
  46.     public void parserXml(String fileName) {  
  47.           File inputXml=new File(fileName);  
  48.           SAXReader saxReader = new SAXReader();  
  49.          try {  
  50.              Document document = saxReader.read(inputXml);  
  51.              Element employees=document.getRootElement();  
  52.             for(Iterator i = employees.elementIterator(); i.hasNext();){  
  53.                   Element employee = (Element) i.next();  
  54.                  for(Iterator j = employee.elementIterator(); j.hasNext();){  
  55.                       Element node=(Element) j.next();  
  56.                       System.out.println(node.getName()+":"+node.getText());  
  57.                   }  
  58.   
  59.              }  
  60.          } catch (DocumentException e) {  
  61.              System.out.println(e.getMessage());  
  62.          }  
  63.       System.out.println("dom4j parserXml");  
  64.      }  
  65.   
  66.   
  67. }  
  68. 4.JDOM生成和解析XML  
  69. 为减少DOM、SAX的编码量,出现了JDOM;优点:20-80原则,极大减少了代码量。使用场合:要实现的功能简单,如解析、创建等,但在底层,JDOM还是使用SAX(最常用)、DOM、Xanan文档。  
  70. <pre name="code" class="java">package com.alisoft.facepay.framework.bean;  
  71.   
  72. import java.io.FileNotFoundException;  
  73. import java.io.FileOutputStream;  
  74. import java.io.IOException;  
  75. import java.util.List;  
  76.   
  77. import org.jdom.Document;  
  78. import org.jdom.Element;  
  79. import org.jdom.JDOMException;  
  80. import org.jdom.input.SAXBuilder;  
  81. import org.jdom.output.XMLOutputter;  
  82. /**
  83. *
  84. * @author hongliang.dinghl
  85. * JDOM 生成与解析XML文档
  86. *
  87. */  
  88. public class JDomDemo implements XmlDocument {  
  89.   
  90.     public void createXml(String fileName) {  
  91.        Document document;  
  92.        Element   root;  
  93.        root=new Element("employees");  
  94.        document=new Document(root);  
  95.        Element employee=new Element("employee");  
  96.        root.addContent(employee);  
  97.        Element name=new Element("name");  
  98.        name.setText("ddvip");  
  99.        employee.addContent(name);  
  100.        Element sex=new Element("sex");  
  101.        sex.setText("m");  
  102.        employee.addContent(sex);  
  103.        Element age=new Element("age");  
  104.        age.setText("23");  
  105.        employee.addContent(age);  
  106.        XMLOutputter XMLOut = new XMLOutputter();  
  107.       try {  
  108.          XMLOut.output(document, new FileOutputStream(fileName));  
  109.      } catch (FileNotFoundException e) {  
  110.          e.printStackTrace();  
  111.      } catch (IOException e) {  
  112.          e.printStackTrace();  
  113.      }  
  114.   
  115.      }  
  116.   
  117.     public void parserXml(String fileName) {  
  118.          SAXBuilder builder=new SAXBuilder(false);   
  119.         try {  
  120.              Document document=builder.build(fileName);  
  121.              Element employees=document.getRootElement();   
  122.              List employeeList=employees.getChildren("employee");  
  123.             for(int i=0;i<employeeList.size();i++){  
  124.                  Element employee=(Element)employeeList.get(i);  
  125.                  List employeeInfo=employee.getChildren();  
  126.                 for(int j=0;j<employeeInfo.size();j++){  
  127.                      System.out.println(((Element)employeeInfo.get(j)).getName()+":"+((Element)employeeInfo.get(j)).getValue());  
  128.                       
  129.                  }  
  130.              }  
  131.          } catch (JDOMException e) {  
  132.           
  133.              e.printStackTrace();  
  134.          } catch (IOException e) {  
  135.           
  136.              e.printStackTrace();  
  137.          }   
  138.   
  139.      }  
  140. }  
  141.    </pre>

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