SAX

本文介绍了SAX解析器,它是公共领域软件和API,与DOM不同,SAX按顺序处理XML文档,而DOM将整个文档加载到内存以树结构存储,体现了性能与编程简易性的权衡。还说明了在Java中应用SAX解析XML的步骤,如创建事件处理程序、SAX解析器等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Introdcution

SAX(unlike DOM)is not a W3C Recommendation.It is a public domain software,created by members of the XML-DEV mailing list, led by David Megginson. It is important to realize that SAX is an API,providing interfaces for parsers to implement.Therefore,SAX by itself can not be used to parse documents.

SAX  versus DOM

SAX  parsers process the XML document sequentially while DOM parsers load the entire document into memory store it in a tree structure.Therefore, SAX versus DOM, in other words, is the tradeoff:performance versus easy programming.(the different of  these parsers process between SAX and DOM is not mentioned detailedly.)

Understand SAX and Apply SAX(with Java)

SAX gives you access to the information in your XML document,not as a tree of nodes,but as a sequence of events.All that the parser does is to read through the XML document and fire some events depending on the tags encountered.

Then apply SAX in programing must follow following steps:

1. Create an event handler

2. Create a SAX parser

3. Assign event handler to SAX parser

4. Parse documet and send events to handler

1.Create an event handler

you must create a handler before application use the SAX to parse XML document. SAX provide a class called DefaultHandler, you can extend this class, and override the methods.

DefaultHandler is in package org.xml.sax.helpers.It implements ContentHandler,DTDHandler,EntityHandler and ErrorHandler.To use this class is imprtant to your application.

2.Create a SAX parser

there are 3 approachs to create a SAX parser. Invoke the driver directly, Specify the driver when run application  and Tranfer driver as a argument of the createXMLreader()

Invoke the driver directly can like this:

try {

      XMLReader xmlReader= new org.apache.xerces.parsers.SAXparser();

} cache (Exception e) {

     system.out.println("can't create the parser" + e.getMessage())

}

note: suppose that use the xerces parser.

Specify the driver when run application use command line to tranfer the driver to application.It's not good way, I think.

The last approach is wide used.

 

not accomplish   continue....

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值