Java xml处理概述

在Java中,对xml的处理模型主要有两种,一种DOM,一种是SAX。DOM是w3c提出的一个xml处理模型,需要在内存中保存整个XML文档的相关信息,好处是灵活性强,可以对XML文档的任何部分以任何顺序进行处理,缺点是耗内存。SAX是开源社区提出的一套API,与DOM不同,它不是规范,但是由于其使用的广泛性,可以把SAX看作是一个事实上的规范。SAX把XML文档当作一个流来处理,而不是把整个XML文档读到内存之后再处理,所以占用的内存非常少。但是SAX只支持一次性地读取,也就是说你无法回到之前处理过的XML节点,而且SAX只支持读取XML,而支持XML的写入。同时,SAX以事件的形式向客户端报告解析XML过程中遇到的各种合法的XML结构(如果标签,属性,内容等等),SAX以推(push)的方式来报告事件,所以客户端无法主动调用SAX的API来获取XML文档的内容,并且在SAX解析XML文档的过程中,调用者线程必须等待,知道整个解析过程完成为止。SAX 2.0和DOM level 2和3的都已经包含进JDK。
由于DOM和SAX都是API形式的规范,所以可以有很多不同的实现,为了支持不同的实现,DOM和SAX都提供了一些工厂方法来查找具体的实现者,同时在工厂方法中也规定了如何查找相应的实现者以及查找的顺序。

对于DOM来说,主要的工厂方法是org.w3c.dom.bootstrap.DOMImplementationRegistry.newInstance()方法,在JDK的文档中,是这样描述其实现的:
Obtain a new instance of a DOMImplementationRegistry. The DOMImplementationRegistry is initialized by the application or the implementation, depending on the context, by first checking the value of the Java system property org.w3c.dom.DOMImplementationSourceList and the the service provider whose contents are at "META_INF/services/org.w3c.dom.DOMImplementationSourceList" The value of this property is a white-space separated list of names of availables classes implementing the DOMImplementationSource interface. Each class listed in the class name list is instantiated and any exceptions encountered are thrown to the application.
从jdk的说明来看,首先会检查org.w3c.dom.DOMImplementataionSourceList这个系统属性,然后检查API实现者提供的jar包中的META_INF/service/org.w3c.dom.DOMImplementationSourceList文件,系统属性和文件中列出了实现了DOMImplementationSource接口的所有的实现类,系统属性和jar保重的文件(Java Service Provider规范)只取一个,优先使用系统属性。在得到了DOMImplementationRegistry实例之后,就可以调用它的getDOMImplementation(String features)的方法来获取DOMImplementation接口的实现类,DOMImplementation接口提供了方法用来创建Doument对象。

对于SAX来说,主要的工厂方法是org.xml.sax.helpers.XMLReaderFactory.createXMLReader()方法,在jdk中,其实现描述如下:
Attempt to create an XMLReader from system defaults. In environments which can support it, the name of the XMLReader class is determined by trying each these options in order, and using the first one which succeeds:
1.If the system property org.xml.sax.driver has a value, that is used as an XMLReader class name.
2.The JAR "Services API" is used to look for a class name in the META-INF/services/org.xml.sax.driver file in jarfiles available to the runtime.
3.SAX parser distributions are strongly encouraged to provide a default XMLReader class name that will take effect only when previous options (on this list) are not successful.
3.Finally, if ParserFactory.makeParser() can return a system default SAX1 parser, that parser is wrapped in a ParserAdapter. (This is a migration aid for SAX1 environments, where the org.xml.sax.parser system property will often be usable.)
In environments such as small embedded systems, which can not support that flexibility, other mechanisms to determine the default may be used.
Note that many Java environments allow system properties to be initialized on a command line. This means that in most cases setting a good value for that property ensures that calls to this method will succeed, except when security policies intervene. This will also maximize application portability to older SAX environments, with less robust implementations of this method.
过程基本和DOM工厂类的查找过程类似,只不过系统属性和文件名改成了org.xml.sax.driver。通过createXMLReader方法返回的XMLReader实例可以被用来对XML文档进行解析。

为了统一对DOM和SAX两种不同的XML模型的处理,Sun在其JDK中加入了一个JAXB规范。通过这个规范,使得替换DOM或者SAX的解析器不至于调整应用程序代码。通过JAXB创建DOM和SAX解析器的类在javax.xml.parsers中,具体使用方式可以查看JDK文档。另外,由于通过DOMImplementationRegistry、DOMImplementation接口只能创建Document而不能根据XML生成Document对象,所以,如果要通过现有的XML来生成Document对象只能通过JAXP的API或者DOM规范实现方提供的具体方法。

最后是Sun提出的Streaming API,Streaming API与SAX类似,也是通过流和事件的方式来处理XML文档。与SAX相比,主要有两方面的区别:
1.StAX(The Streaming API for XML)不仅支持读取XML文档,同时也支持创建XML文档
2.StAX以拉(pull)的方式提供XML文档的相关信息,也就是说,StAX对XML的解析过程由客户端程序控制。StAX提供的XMLStreamReader和XMLEventReader接口都提供了next()方法,使得客户端程序能够获取XML文档中的内容。关于XMLStreamReader和XMLEventReader两者的区别,参考:The Java Web Services Tutorial


StAX中的工厂类主要有三个,分别是XMLEventFactory、XMLInputFactory和XMLOutputFactory,都为与javax.xml.stream包下面。这些工厂类提供的工厂方法用来获取相应的XML读写类。具体的使用方式,可以查看JDK的文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值