`
i_bugs
  • 浏览: 3862 次
社区版块
存档分类
最新评论

i_bugs study: dom4j解析XML

    博客分类:
  • java
阅读更多

    简介:
    1. JAVA XML API
    2. Dom4j is a simple and flexible open source library for working with XML, XPath and XSLT on the Java platform using the Java Collections Framework with full integration with DOM, SAX and JAXP.



    概念:
    1. XML
    全称:Extensible Markup Language,可扩展标记语言
    定义:用于标记电子文件使其具有结构性标记语言
    作用:提供统一的方法来描述和交换独立于应用程序或供应商的结构化数据

    2. DOM
    全称:Document Object Model,文档对象模型
    定义:The Document Object Model is a platform-and language-neutral interface that will allow programs and scripts to dynamically access and update the content, structure and style of documents. The document can be further processed and the results of that processing can be incorporated back into the the presented page.
    文档对象模型作为与平台和语言无关的接口,允许程序和脚本语言动态访问和更新文档的内容,结构和样式。使用文档对象模型的文件可以被进一步处理,同时处理的结果可以合并到已显示的页面中。


    为什么选择DOM4J解析器
    1. 常见解析器
    DOM是基于平台、语言无关的W3C标准。基于树的层次,其优点是可以移植,编程容易,开发人员只需要调用建树的指令。缺点是加载大文件不理想。

    SAX是基于事件模型的,它在解析XML文档的时候可以触发一系列的事件,当发现给定的tag时候,他可以激活一个回调方法,告诉该方法制定的标签已经找到。类似与流媒体的解析方式,所以在加载大文件的时候效果不错。

    JDOM是想成为Java特定文档模型。简化与XML的交互并且比使用DOM实现更快。使用的是具体类不使用借口,运用大量的Collection类,方便程序员。To provide a complete, Java-based solution for accessing, manipulating, and outputting XML data from Java code.

    DOM4J是一个独立的开发结果,非常优秀的Java XML API, 具有性能优异、功能强大和极端易用的特点,同时是一个开源工具。

    2. 原因
    DOM4J性能最好,连Sun的JAXM也在用DOM4J。目前许多开源项目中大量采用DOM4J,例如Hibernate也用DOM4J来读取XML配置文件。如果不考虑可移植性,那就采用DOM4J.

[list]
DOM4J使用
1. 方法列表

创建SAX解析器,以便通过解析器将XML转换成DOM4J树形模型。
//SAXReader creates a DOM4J tree from SAX parsing events.
//The actual SAX parser that is used by this class is configurable so you can use your favourite SAX parser if you wish. 事实上本类所使用的SAX解析器是可以配置的,因此可以选择你钟爱的SAX解析器。
//DOM4J comes configured with its own SAX parser so you do not need to worry about configuring the SAX parser. DOM4J来配置自己的SAX解析器,因此你不必要担心如何配置SAX解析器了。
new SAXReader();


SAXReader提供了读取File文件的方法,更多方法见源码。
//Reads a Document from the given File.
//We cannot convert the file to an URL because if the filename contains '#' characters, there will be problems with the URL in the InputSource
read(File file)


DOM4J Document提供了获取根节点方法
//Returns the root Element for this document.
Element org.dom4j.Document.getRootElement()


获取根元素名称
//getName returns the name of this node. This is the XML local name of the element, attribute, entity or processing instruction. For CDATA and Text nodes this method will return null.
String org.dom4j.Node.getName()


通过名称获取元素
//Returns the first element for the given local name and any namespace.
Element org.dom4j.Element.element(String name)


获取直接子元素对象List
//Returns the elements contained in this element. If this element does not contain any elements then this method returns an empty list. The list is backed by the element such that changes to the list will be reflected in the element though the reverse is not the case. 
List org.dom4j.Element.elements()


获取元素文本
//Returns the text value of this element without recursing through child elements. This method iterates through all Text, CDATA and Entity nodes that this element contains and appends the text values together.
String org.dom4j.Element.getText()


其它方法略(如取得元素属性、增加、删除元素等方法),详情参看源码
2. 实例
下载jar包,见Reference [1]



    Reference
    1. http://sourceforge.net/projects/dom4j/
    2. http://www.w3.org/DOM/
    3. http://jdom.org/
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics