解析XML的几种方式

来源:互联网 发布:淘宝买家秀百度云 编辑:程序博客网 时间:2024/06/06 03:44
  XML现在已经成为一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便。对于XML本身的语法知识与技术细节,需要阅读相关的技术文献,这里面包括的内容有DOM(Document Object Model),DTD(Document Type Definition),SAX(Simple API for XML),XSD(Xml Schema Definition),XSLT(Extensible Stylesheet Language Transformations),具体可参阅w3c官方网站文档http://www.w3.org获取更多信息。

XML在不同的语言里解析方式都是一样的,只不过实现的语法不同而已。基本的解析方式有两种,一种叫SAX,另一种叫DOM。SAX是基于事件流的解析,DOM是基于XML文档树结构的解析。假设我们XML的内容和结构如下:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <employees>   
  3. <employee>   
  4. <name>ddviplinux</name>   
  5. <sex>m</sex>   
  6. <age>30</age>   
  7. </employee>   
  8. </employees>  
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <employees>   
  3. <employee>   
  4. <name>ddviplinux</name>   
  5. <sex>m</sex>   
  6. <age>30</age>   
  7. </employee>   
  8. </employees>  

本文使用JAVA语言来实现DOM与SAX的XML文档生成与解析。 
首先定义一个操作XML文档的接口XmlDocument 它定义了XML文档的建立与解析的接口。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /**  
  2.  
  3. * @author hongliang.dinghl  
  4. * 定义XML文档建立与解析的接口  
  5. */   
  6. public interface XmlDocument {   
  7. /**  
  8. * 建立XML文档  
  9. * @param fileName 文件全路径名称  
  10. */   
  11. public void createXml(String fileName);   
  12. /**  
  13. * 解析XML文档  
  14. * @param fileName 文件全路径名称  
  15. */   
  16. public void parserXml(String fileName);   
  17. }   
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /**  
  2.  
  3. * @author hongliang.dinghl  
  4. * 定义XML文档建立与解析的接口  
  5. */   
  6. public interface XmlDocument {   
  7. /**  
  8. * 建立XML文档  
  9. * @param fileName 文件全路径名称  
  10. */   
  11. public void createXml(String fileName);   
  12. /**  
  13. * 解析XML文档  
  14. * @param fileName 文件全路径名称  
  15. */   
  16. public void parserXml(String fileName);   
  17. }   


1.DOM生成和解析XML文档

为 XML 文档的已解析版本定义了一组接口。解析器读入整个文档,然后构建一个驻留内存的树结构,然后代码就可以使用 DOM 接口来操作这个树结构。优点:整个文档树在内存中,便于操作;支持删除、修改、重新排列等多种功能;缺点:将整个文档调入内存(包括无用的节点),浪费时间和空间;使用场合:一旦解析了文档还需多次访问这些数据;硬件资源充足(内存、CPU)。


[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. import java.io.FileInputStream;   
  2. import java.io.FileNotFoundException;   
  3. import java.io.FileOutputStream;   
  4. import java.io.IOException;   
  5. import java.io.InputStream;   
  6. import java.io.PrintWriter;   
  7. import javax.xml.parsers.DocumentBuilder;   
  8. import javax.xml.parsers.DocumentBuilderFactory;   
  9. import javax.xml.parsers.ParserConfigurationException;   
  10. import javax.xml.transform.OutputKeys;   
  11. import javax.xml.transform.Transformer;   
  12. import javax.xml.transform.TransformerConfigurationException;   
  13. import javax.xml.transform.TransformerException;   
  14. import javax.xml.transform.TransformerFactory;   
  15. import javax.xml.transform.dom.DOMSource;   
  16. import javax.xml.transform.stream.StreamResult;   
  17. import org.w3c.dom.Document;   
  18. import org.w3c.dom.Element;   
  19. import org.w3c.dom.Node;   
  20. import org.w3c.dom.NodeList;   
  21. import org.xml.sax.SAXException;   
  22. /**  
  23.  
  24. * @author hongliang.dinghl  
  25. * DOM生成与解析XML文档  
  26. */   
  27. public class DomDemo implements XmlDocument {   
  28. private Document document;   
  29. private String fileName;   
  30. public void init() {   
  31. try {   
  32. DocumentBuilderFactory factory = DocumentBuilderFactory   
  33. .newInstance();   
  34. DocumentBuilder builder = factory.newDocumentBuilder();   
  35. this.document = builder.newDocument();   
  36. catch (ParserConfigurationException e) {   
  37. System.out.println(e.getMessage());   
  38. }   
  39. }   
  40. public void createXml(String fileName) {   
  41. Element root = this.document.createElement("employees");   
  42. this.document.appendChild(root);   
  43. Element employee = this.document.createElement("employee");   
  44. Element name = this.document.createElement("name");   <li class="alt" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-hei
0 0