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

被折叠的 条评论
为什么被折叠?



