Use SAX to process XML documents

xiaoxiao2021-03-06  107

Use SAX to process XML documents http://www-900.ibm.com/developerWorks/cn/xml/x-saxhandle/index.shtml

content:

SAX's basic situation understands and uses SAX an instance using defaulthandler Using filter Some Identity issues SAX and DOM comparison Appendix SAX's copyright statement about the author

Wang Xiaoqiang (Forff@sina.com)

November 2001

Here I will introduce you to another important interface SAX (Simple API for XML) to process the XML document. These include its basic case, its API, a development instance, some need to pay attention to problems in actual development, and comparison with the DOM.

SAX's basic case SAX is also an interface to access the XML document as a DOM. SAX is an abbreviation for Simple API for XML. It is not as recommended by W3C as DOM. It develops maintenance by members of the XML-DEV mail list, a public Domain software for David Megginson Leaders (David@meginson.com). SAX is a thorough free software that gives the author abandon all rights to it, and it is also licensed for any purpose (in the article last appendicle its copyright statement). The version of SAX is now developed to 2.0. Support for Namespaces is added to this latest release, and can be fully configured to the parser by settings for Features and Properties, including setting the parser to validate the document, and how To handle element names with namespaces, etc.. The interface in SAX1 is no longer used, which will only discuss the development of SAX2. In this article, SAX just refers to SAX 2. In addition, all examples of this article are written in Java, and the SAX parser is also used by Java versions. Like the DOM, SAX is not an XML document parser that can actually use, but a collection of interfaces and help classes that are compatible with SAX parsers. If you want to use SAX, you must meet the following requirements:

The system contains Java 1.1 or higher. In Java ClassPath, you include entering your SAX class library. In Java ClassPath, you include a compatible SAX XML parser class library you want to use. There are many parsers that achieve SAX, such as apache's Xerces, Oracle XML Parser, and more. The examples used in this article are all Xerces parsers, you can get it from http://xml.apache.org. Let's download the Xerces.jar file and join it into the classpath so that we have established a good environment (SAX interface already contains in Xerces.jar, so you don't have to deliberately look for SAX class libraries). There are two packages, org.xml.sax. Helper in the SAX API. ORG.XML.SAX mainly defines some of the foundation interfaces of SAX, such as XmlReader, ContentHandler, Errorhandler, DTDHandler, EntityResolver, etc. In Org.xml.sax.Helper is some help classes for developers, such as the default implementation of all processor interface help class DEFAULTHANDLER, which is convenient for developers to create XMLReader's XMLReaderFactory class. There are also some interfaces that apply to SAX1 in these two packages, and there are several classes that are just to facilitate applications that will be developed on SAX1 to SAX2, which is not involved in this article. Below is the interfaces and classes we have to pay attention to:

Package Org.xml.sax Introduction Interfaces Interface Attributes defines a list of property list interfaces for access to an attribute list. ContentHandler handles events generated when the content of the document is analyzed. DTDHandler handles the corresponding events when parsing DTD. EntityResolver handles external entities. ErrorHandler handles the document error event encountered during the parsing process. Locator is a locator interface prepared for the location of the content event generated in the parser in the parsing. XMLFilter provides a filter interface for easy application development. XmlReader Any SAX2 parser is to implement this interface. This interface allows the application to set or find Features and Properties, register various event processors, and start parsing documents. ClassesInputSource is ready for an XML entity. ExceptionssaxException packs a general SAX error and warning. SAXNOTRECognizedException is throwing an exception thrown without some identity. SAXNOTSUPPORTEDEXCEPTION is an exception thrown for a certain action. SAXPARSEEXCEPTION Packages an error or warning about XML parsing. Package Org.xml.sax.Helpers Help class The package CLASSES class AttributeSimpl for the default implementation of the Attributes interface NameSpaceSupport provides namespace support. DEFAULTHANDLER defaults to four processor interfaces for users to develop, often used during development. LocatorIMPL provides an implementation of the implementation of the Locator interface to the implementation of the filter interface, and uses filters to develop, inheriting this class is very convenient. XmlReaderFactory is available for easy creation of different XMLReaders. It is often used. Understanding and using SaxSax design is completely different from DOM! The DOM handling XML document is based on the resolution of the XML document into a tree model, and puts into memory. The SAX is an event-driven processing mode that converts the XML document into a series of events, which determines how to handle it by a separate event processor. To learn how to use the SAX API to process the XML document, you will first introduce the event-driven processing mode based on SAX. This event-based processing mode is a common programming mode that is widely used in GUI design. There is a figure in Java's AWT, SWING, and Java Beans. And SAX's event-driven processing mode is very similar to those above. The event-based processing mode is mainly working around the event source and event processor (or listener). A object that can generate events is called an event source, and an object that can be responded to an event is called an event processor. The event source and event processor are connected by registering methods in the event processor in the event source. Thus, when the event source generates an event, the corresponding processing method of the event processor is called, and an event has been processed. Of course, when the event source calls the specific method in the event processor, it is passed to the status information of the event processor's corresponding event, so the event processor can determine its own behavior according to event information. In the SAX interface, the event source is the XMLReader in the org.xml.sax package, which starts parsing the XML document through the PARSE () method and generates an event according to the document content. The event processor is the ContentHandler, DTDHandler, Errorhandler, DTDHandler, Errorhandler, and EntityResolver. They handle different types of events generated in the resolution process (where DTDHandler is used for resolving documents DTD). The connection of the event source XMLReader and the four event processors is done by the corresponding event processor registration method set *** () in the XMLReader. For details, see the following table:

Processor Name The event registration method org.xml.sax.contentHandler All events related to document content: The start of the document and ends the beginning of the XML element and ends the negligible entity namespace prefix mapping start and end processing instruction character data and The negligible spaces in XMLReader (ContentHandler Handler) method org.xml.sax.Errorhandler processes errors when the XML document parses. If an application does not register an error processor class, an unpredictable parser behavior occurs. setErrorHandler (ErrorHandler handler) org.xml.sax.DTDHandler generated when processing the document parsing DTD corresponding event setDTDHandler (DTDHandler handler) org.xml.sax.EntityResolver processing external entities setEntityResolver (EntityResolver resolver) in the four processor interface In the most important thing for us is the ContentHandler interface. Let's take a look at the description of the method:

Methods Name Method Description public void setDocumentLocator (Locator locator) to set a target locator can locate the document content event location public void startDocument () throws SAXException for processing documents to parse event public void endDocument () throws SAXException for processing document parsing end event public void startPrefixMapping (java.lang.String prefix, java.lang.String uri) throws SAXException prefix mapping process for a start event can be obtained and the prefix name uripublic void endPrefixMapping (java.lang.String pointed parameters from prefix) throws SAXException prefix mapping for processing end event can be a prefix name public void startElement (java.lang.String namespaceURI from parameter, java.lang.String localName, java.lang.String qName, Attributes atts) throws SAXException processing Elements starting events, from the parameters, you can get the namespace of the element in the name of the URI, element name, attribute list, etc. Treatment element end events, from the parameters, you can get the URI of the namespace where the element is located, the element name and other information PUBLIC VOID CHARACTERS (CHAR [] CH, INT LENGT, INT Length) THROWS SAXEXCEPTION handles the character content of the element, from the parameters you can get content Public void IgnorableWhitespace (Char [] ch, int start, intling throws SaxException processing element's negligible space PUBLIC VOID Processinginstruction (java.l Ang.String Target, java.lang.string data) THROWS SAXEXCEPTION Processing Events Generated Informed Processing Instructions Here are the methods in org.xml.sax.xmlReader, then let us see a specific example. XmlReader is an interface to all compatible with SAX2 parsers, which start parsing documents by it, and calls its registration method to register a variety of event processors. Please see the following table:

The method describes the method name public Boolean getFeature (java.lang.String name) throws SAXNotRecognizedException, SAXNotSupportedException obtain a feature value of the public void setFeature (java.lang.String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException set value of a feature, For example, if you need a parser support to verify the document, then call this method. MyReader.SetFeature (http://xml.org/sax/features/validation, princise); where MyReader is an instance of XMLReader. public java.lang.Object getProperty (java.lang.String name) throws SAXNotRecognizedException, SAXNotSupportedException return a property value public void setProperty (java.lang.String name, java.lang.Object value) throws SAXNotRecognizedException, SAXNotSupportedException set a property of value public void setEntityResolver (EntityResolver resolver) registration processing external entity EntityResolverpublic EntityResolver getEntityResolver () to get registered in the system EntityResolverpublic void setDTDHandler (DTDHandler handler) registration process DTD parsing events DTDHandlerpublic DTDHandler getDTDHandler () to get DTDHandlerpublic void setContentHandler registered in the system ( ContentHandler handler) registration process the XML document content parsing events ContentHandlerpublic ContentHandler getContentHandler () to get ContentHandlerpublic void setErrorHandler (ErrorHandler handler) registered in the system registration process document parsing error events ErrorHandlerpublic ErrorHandler getErrorHandler () to get registered in the system ErrorHandlerpublic void parse (InputSource INPUT) THROWS JAVA.IOEXCEPTION, SAXEXCEPTION begins to resolve an XML document. Public void parse (java.lang.string systemid "throws java.io ioException, SAXException begins to resolve an XML document identified using the system identifier. This method is just a shortcut to the above method. It is equivalent to: Parse (New INPUTSOURCE (SystemID)); an instance allows us to see how the application using SAX parsing XML documents is created. Here is the XML document processed in the application. To illustrate the support of SAX to name space, I specially added an element with a namespace here, which generates the corresponding prefix mapping start and end events.

Java 2 Programming Detailed </ Title></p> <p><Price Money Unit = "RMB"> 150 </ Price> <Purchase Date> 2000, 1,24 </ Purchase Date></p> <p></ Book></p> <p></ Technical Books></p> <p><book: literature book xmlns: book = "http://javausr.com" /></p> <p><Historical books /></p> <p></ My bookshelf> The example of the example here simply prints the event information that will encounter. We first implement the ContentHandler interface to handle events related to document content during the XML document parsing process. The code is as follows MyContentHandler.java: qu.javausr.saxexample;</p> <p>Import org.xml.sax.attribute;</p> <p>Import org.xml.sax.contenthandler;</p> <p>Import org.xml.sax.locator;</p> <p>Import org.xml.sax.saxexception;</p> <p>Public class mycontenthandler imports contenthandler {</p> <p>Private stringbuffer buf;</p> <p>Public void setDocumentlocator (locator location) {</p> <p>}</p> <p>Public void startdocument () throws saxexception {</p> <p>BUF = New stringbuffer ();</p> <p>System.out.println ("******* Start resolving document *********************</p> <p>}</p> <p>Public void enddocument () throws saxexception {</p> <p>System.out.println ("******* resolved document end *************");</p> <p>}</p> <p>Public void processinginstruction (String Target, String Instruction)</p> <p>Throws saxception {</p> <p>}</p> <p>Public void startprefixmapping (string prefix, string URI) {</p> <p>System.out.println ("/ N prefix mapping: prefix " Start! " " Its URI is: " URI);</p> <p>}</p> <p>Public void endprefixmapping (string prefix) {</p> <p>System.out.println ("/ N prefix mapping: prefix " end! ");</p> <p>}</p> <p>Public void StartElement (String Namespaceuri, String Localname,</p> <p>String Fullname, Attributes Attributes)</p> <p>Throws saxception {</p> <p>System.out.println ("/ n element:" "[" fullname "]" "Start resolution!");</p> <p>// Print the property information</p> <p>For (int i = 0; i <attibutes.getlength (); i ) {</p> <p>System.out.println ("/ t attribute name:" Attributes.getlocalName (i)</p> <p> "Attribute value:" Attributes.getValue (i));</p> <p>}</p> <p>}</p> <p>Public void endelement (String Namespaceuri, String Localname, String Fullname)</p> <p>Throws saxception {</p> <p>// Print out of the empty element content and empty StringBuffer</p> <p>String nullstr = "";</p> <p>IF (! buf.tostring (). Trim (). Equals (nullstr)) {</p> <p>System.out.println ("/ t content is:" buf.tostring (). TRIM ());</p> <p>}</p> <p>BUF.SETLENGTH (0);</p> <p>// Print element parsing end information</p> <p>System.out.println ("Element:" "[" FullName "]" "parsing end!");</p> <p>}</p> <p>Public void characters (char [] Chars, int Start, INT LENGTH)</p> <p>Throws saxception {</p> <p>// Putting the contents of the element to StringBuffer</p> <p>BUF.Append (Chars, Start, Length);</p> <p>}</p> <p>Public void ignorablewhitespace (char [] Chars, int Start, int LENGTH)</p> <p>Throws saxception {</p> <p>}</p> <p>Public void Skippedentity (String Name) throws saxexception {</p> <p>}</p> <p>} The following will create a mysaxapp.java class that is taking an Xerces parser to implement the XMLReader interface and use the MyContentHandler that has just created.</p> <p>Import org.xml.sax.xmlreader;</p> <p>Import org.xml.sax.helpers.xmlreaderfactory;</p> <p>Import org.xml.sax.contenthandler;</p> <p>Import org.xml.sax.saxexception;</p> <p>Import java.io.ioException;</p> <p>Public class mysaxapp {</p> <p>Public static void main (String [] args) {</p> <p>IF (args.length! = 1) {</p> <p>System.out.println ("Enter: Java MySaxApp");</p> <p>System.exit (0);</p> <p>}</p> <p>Try {</p> <p>// Initialize Reader</p> <p>XmlReader Reader = XmlReaderFactory.createxmlReader</p> <p>("Org.apache.xerces.Parsers.saxparser");</p> <p>// Create an instance of ContentHandler</p> <p>ContentHandler ContentHandler = new mycontenthandler ();</p> <p>// Register an instantiated ContentHandler in Reader</p> <p>Reader.SetContentHandler (ContentHandler);</p> <p>// Start parsing document</p> <p>Reader.Parse (ARGS [0]);</p> <p>} catch (ioexception e) {</p> <p>System.out.println ("When you read a document:" E.getMessage ());</p> <p>} catch (saxException e) {</p> <p>System.out.println ("Resolving the document:" E.GetMessage ());</p> <p>}</p> <p>}</p> <p>} Let's take a look at the results:</p> <p>D: / SAX / CLASSES> Java com.javausr.saxexample.mysaxapp d: /book.xml ******* Start Resolution Document *******</p> <p>Element: [My Books] begins to resolve!</p> <p>Element: [Technical Book] Start analysis!</p> <p>Element: [Book] Start analysis!</p> <p>Element: [Title] Start analysis!</p> <p>The content is: Java 2 programming detailed</p> <p>Element: [book name] parsing end!</p> <p>Element: [Price] Start analysis!</p> <p>Property Name: Money unit attribute value: RMB</p> <p>The content is: 150</p> <p>Element: [Price] End!</p> <p>Element: [Purchase Date] Start resolution!</p> <p>The content is: 2000, 1, 24</p> <p>Element: [Buy Date] End!</p> <p>Element: [Book] End!</p> <p>Element: [Technical Book] End!</p> <p>Prefix mapping: Book Start! Its URI is: http://javausr.com</p> <p>Element: [Book: Literature Book] Start analysis!</p> <p>Element: [Book: Literature Book] End!</p> <p>Prefix mapping: Book ends!</p> <p>Element: [History Book] Start analysis!</p> <p>Element: [Historical Book] End!</p> <p>Element: [My Books] End!</p> <p>******* The end of the resolution document ******* The above is to use SAX to resolve the basic process of an XML document, but MyContentHandler only handles events related to document content in the process, if the resolution The error is that we need to implement an Errorhandler interface. If you do not register an error handler to handle, the error event will not be reported, and the parser will unpredictable behavior. The errors generated during the parsing process are divided into three classes, which are Warning, Error, and Fatalerror, which means that there are such an error event in ERRORHANDLER. Here is an introduction to these three error handling methods:</p> <p>Method Name Method Introduction Warning () SAX parser will use this method to report an incorrect state of the non-error (ERROR) or fatal error defined in the XML1.0 specification. Nothing for this error default behavior is not done. The SAX parser must continue to provide a normal resolution event after calling this method: The application should continue to process the document. ERROR () This method corresponds to the "Error" concept defined in the 1.2 section of the W3C XML 1.0 specification. For example, a parser with validity verification uses this method to report violations of validity verification. A parser with validity verification uses this method to report that violation of some sexual constraints. The default behavior is nothing. The SAX parser must continue to provide a normal resolution event after calling this method: The application should continue to process the document. If the application does not do so, the parser reports a fatal error even if the XML1.0 specification is not required. Fatalerror () This method corresponds to the "Fatal Error" concept defined in the 1.2 part of the W3C XML1.0 specification. For example, a parser uses this method to report a situation in violation of the format. After the parser calls this method, the application must indicate that this document is unused, and should be only processed to collect an error message (if needed): In fact, once the SAX parser can stop after this method Report any event. Here is a Myerrorhandler.java class that implements an Errorhandler interface: package com.javausr.saxexample;</p> <p>Import org.xml.sax.errorhandler;</p> <p>Import org.xml.sax.saxparseException;</p> <p>Import org.xml.sax.saxexception;</p> <p>Public class myerrorhandler imports errorhandler {</p> <p>Public void Warning (SaxParseException Exception) {system.out.println ("******* warning *******");</p> <p>System.out.println ("/ T line: / t" exception.getLineNumber ());</p> <p>System.out.println ("/ T Column: / T" Exception.getColumnNumber ());</p> <p>System.out.println ("/ T error message: / t" exception.getMessage ());</p> <p>System.out.println ("********************");</p> <p>}</p> <p>Public void error (SAXPARSEEXCEPTION "throws saxexception {</p> <p>System.out.println ("******* error ******");</p> <p>System.out.println ("/ T line: / t" exception.getLineNumber ());</p> <p>System.out.println ("/ T Column: / T" Exception.getColumnNumber ());</p> <p>System.out.println ("/ T error message: / t" exception.getMessage ());</p> <p>System.out.println ("********************");</p> <p>}</p> <p>Public void fatalerror (SAXPARSEEXCEPTION exception "SAXEXCEPTION {</p> <p>System.out.println ("**************");</p> <p>System.out.println ("/ T line: / t" exception.getLineNumber ());</p> <p>System.out.println ("/ T Column: / T" Exception.getColumnNumber ());</p> <p>System.out.println ("/ T error message: / t" exception.getMessage ());</p> <p>System.out.Println ("*****************************************************************************</p> <p>}</p> <p>} We also have to make some modifications to the mysaxapp.java class (some of the blue marked in the source code) make it use myerrorhandler.java: quhip com.javausr.saxexample;</p> <p>Import org.xml.sax.xmlreader;</p> <p>Import org.xml.sax.helpers.xmlreaderfactory;</p> <p>Import org.xml.sax.contenthandler;</p> <p>// introduce Errorhandler</p> <p>Import org.xml.sax.errorhandler;</p> <p>Import org.xml.sax.saxexception;</p> <p>Import java.io.ioException;</p> <p>Public class mysaxapp {</p> <p>Public static void main (String [] args) {</p> <p>IF (args.length! = 1) {</p> <p>System.out.println ("Enter: Java MySaxApp");</p> <p>System.exit (0);</p> <p>}</p> <p>Try {</p> <p>// Initialize Reader</p> <p>XmlReader Reader = XmlReaderFactory.createxmlReader ("Org.Apache.xerces.Parsrs.saxparser");</p> <p>// Create an instance of ContentHandler</p> <p>ContentHandler ContentHandler = new mycontenthandler ();</p> <p>// Register an instantiated ContentHandler in Reader</p> <p>Reader.SetContentHandler (ContentHandler);</p> <p>// Create an instance of Errorhandler</p> <p>Errorhandler Errorhandler = new myerrorhandler ();</p> <p>// Register an instantiated ErrorHandler in Reader</p> <p>Reader.setdler (ERRORHANDAL);</p> <p>// Start parsing document</p> <p>Reader.Parse (ARGS [0]);</p> <p>} catch (ioexception e) {</p> <p>System.out.println ("When you read a document:" E.getMessage ());</p> <p>} catch (saxException e) {</p> <p>System.out.println ("Resolving the document:" E.GetMessage ());</p> <p>}</p> <p>} Let's make people to create some errors to check our error processor work. Delete the closed tag of the element <purchase date>, which will generate a FATAL ERROR, the following is the execution result: D: / SAX / CLASSES> Java com.javausr.saxexample.mysaxapp D: /book.xml</p> <p>******* Start parsing documents *******</p> <p>Element: [My Books] begins to resolve!</p> <p>Element: [Technical Book] Start analysis!</p> <p>Element: [Book] Start analysis!</p> <p>Element: [Title] Start analysis!</p> <p>Element: [Title] Start analysis!</p> <p>The content is: Java 2 programming detailed</p> <p>Element: [book name] parsing end!</p> <p>Element: [Price] Start analysis!</p> <p>Property Name: Money unit attribute value: RMB</p> <p>The content is: 150</p> <p>Element: [Price] End!</p> <p>Element: [Purchase Date] Start resolution!</p> <p>******** FATAL ERROR ********</p> <p>Dot: 8</p> <p>Columns: 7</p> <p>Error message: The element type "Purchase Date" Must Be Terminated by The matching end-tag "</ purchase date>".</p> <p>****************************</p> <p>Wordess the document error: Stopping After Fatal Error: The Element Type "Buy Date" Must Be Terminated by the matching end-tag "</ Purchase Date>".</p> <p>Now summary how to write SAX-based applications. The general steps are as follows:</p> <p>Implement one or more processor interfaces (ContentHandler, Errorhandler, DTDHandler, or EntityResover). Create an instance of an XMLReader class. In the new XMLReader instance, a plurality of instances of an event processor are registered in a new XMLReader instance to process the document. Using the defaulthandler's current program is complete, there are many places that can be improved. First of all, in MyContentHandler.java we implemented, you will find that there are many ways to do anything, but in order to implement the ContentHandler interface, you have to write them out, so it is very troublesome. The SAX API has taken into account this issue, which provides us with a helpful defaulthandler that makes it easy to implement a variety of processor interfaces in its org.xml.sax.Helper package. This class defaults to achieve the four processor interfaces mentioned above. This way we only need to inherit this class, then cover the event handling method we want to implement. Below we come to create a MyDefaultHandler.java class inherited in inherited DefaultHandler, and then move the event processing method implemented in MyContentHandler.java and MyErrorhandler.java to the MyDefaultHandler.java class, and the method that is not used is not necessary. Here is mydefaulthandler.java: quhipage com.javausr.saxexample; import org.xml.sax. *;</p> <p>Import org.xml.sax.helpers. *;</p> <p>Import java.io. *;</p> <p>Public class mydefaulthandler extends defaulthandler {</p> <p>Private stringbuffer buf;</p> <p>Public void startdocument () throws saxexception {</p> <p>BUF = New stringbuffer ();</p> <p>System.out.println ("******* Start resolving document *********************</p> <p>}</p> <p>Public void enddocument () throws saxexception {</p> <p>System.out.println ("******* resolved document end *************");</p> <p>}</p> <p>Public void startprefixmapping (string prefix, string URI) {</p> <p>System.out.println ("/ N prefix mapping: prefix " Start! " " Its URI is: " URI);</p> <p>}</p> <p>Public void endprefixmapping (string prefix) {</p> <p>System.out.println ("/ N prefix mapping: prefix " end! ");</p> <p>}</p> <p>Public void StartElement (String Namespaceuri, String Localname,</p> <p>String Fullname, Attributes Attributes)</p> <p>Throws saxception {</p> <p>System.out.println ("/ n element:" "[" fullname "]" "Start resolution!");</p> <p>// Print the property information</p> <p>For (int i = 0; i <attibutes.getlength (); i ) {</p> <p>System.out.println ("/ T property name:" attributes.getlocalname (i) "attribute value:" Attributes.getValue (i));</p> <p>}</p> <p>}</p> <p>Public void endelement (String Namespaceuri, String Localname,</p> <p>String fullname)</p> <p>Throws saxception {</p> <p>// Print out of the empty element content and empty StringBuffer</p> <p>String nullstr = "";</p> <p>IF (! buf.tostring (). Trim (). Equals (nullstr)) {</p> <p>System.out.println ("/ t content is:" buf.tostring (). TRIM ());</p> <p>}</p> <p>BUF.SETLENGTH (0);</p> <p>// Print element parsing end information</p> <p>System.out.println ("Element:" "[" FullName "]" "parsing end!");</p> <p>}</p> <p>Public void characters (char [] Chars, int Start, INT LENGTH)</p> <p>Throws saxception {</p> <p>// Putting the contents of the element to StringBuffer</p> <p>BUF.Append (Chars, Start, Length);</p> <p>}</p> <p>Public void Warning (SAXPARSEEXCEPTION) {</p> <p>System.out.println ("******* warning ******");</p> <p>System.out.println ("/ T line: / t" exception.getLineNumber ());</p> <p>System.out.println ("/ T Column: / T" Exception.getColumnNumber ());</p> <p>System.out.println ("/ T error message: / t" exception.getMessage ());</p> <p>System.out.println ("********************");</p> <p>}</p> <p>Public void error (SAXPARSEEXCEPTION "throws saxexception {</p> <p>System.out.println ("******* error ******");</p> <p>System.out.println ("/ T line: / t" exception.getLineNumber ());</p> <p>System.out.println ("/ T Column: / T" Exception.getColumnNumber ());</p> <p>System.out.println ("/ T error message: / t" exception.getMessage ());</p> <p>System.out.println ("********************");</p> <p>}</p> <p>Public void fatalerror (SAXPARSEEXCEPTION exception "SAXEXCEPTION {</p> <p>System.out.println ("**************");</p> <p>System.out.println ("/ T line: / t" exception.getLineNumber ());</p> <p>System.out.println ("/ T Column: / T" Exception.getColumnNumber ()); System.out.Println ("/ T Error Information: / T" Exception.getMessage ());</p> <p>System.out.Println ("*****************************************************************************</p> <p>}</p> <p>} We also have to make corresponding modifications to mysaxapp.java, and modify them in the source code: package com.javausr.saxexample;</p> <p>Import org.xml.sax.xmlreader;</p> <p>Import org.xml.sax.helpers.xmlreaderfactory;</p> <p>// introduce DefaultHandler</p> <p>Import org.xml.sax.helpers.defaulthandler;</p> <p>Import org.xml.sax.saxexception;</p> <p>Import java.io.ioException;</p> <p>Public class mysaxapp {</p> <p>Public static void main (String [] args) {</p> <p>IF (args.length! = 1) {</p> <p>System.out.println ("Enter: Java MySaxApp");</p> <p>System.exit (0);</p> <p>}</p> <p>Try {</p> <p>// Initialize Reader</p> <p>XmlReader Reader = XmlReaderFactory.createxmlReader</p> <p>("Org.apache.xerces.Parsers.saxparser");</p> <p>// Create an instance of defaulthandler</p> <p>DEFAULTHANDLER DefaultHandler = New MyDefaultHandler ();</p> <p>// Register DefaultHandler as ContentHandler in Reader</p> <p>Reader.SetContentHandler (DefaultHandler);</p> <p>// Register DefaultHandler to Errorhandler in Reader</p> <p>Reader.setdler (DefaultHandler);</p> <p>// Start parsing document</p> <p>Reader.Parse (ARGS [0]);</p> <p>} catch (ioexception e) {</p> <p>System.out.println ("When you read a document:" E.getMessage ());</p> <p>} catch (saxException e) {</p> <p>System.out.println ("Resolving the document:" E.GetMessage ());</p> <p>}</p> <p>}</p> <p>} The filter interface org.xml.sax.xmlfilter is implemented in the SAX API, as well as the default to its default org.xmlfilterImpl. Use them can easily develop complex SAX applications. Here you must first introduce the filter design mode. This design pattern is very well understood, just like a process of purifying water. The water in the nature flows through one of the filters to get the last drinking water. These filters, some of the sediments in the water, some are bacteria in the water, and in short, different filters do different tasks. In the application development, we let the transformed object (here the event stream) pass these filter objects to meet the requirements of the requirements after the transformation. In this way, in the help of the filter, we can easily implement a specific function in each filter, creating a complex application. In the application you can construct any multiple filters, connect them together to complete the task. In the SAX API org.xml.sax.xmlfilter interface inherits the org.xml.sax.xmlReader interface. It is different from XMLReader that it does not get events by parsing documents like XMLReader, but acquiring events from other XMLReaders, and of course this also acquires events from other XMLFilters. There are two ways in org.xml.sax.xmlfilter: Method Name Method Description Public Void SetParent (XMLReader Parent) Sets the parent XMLReader. This method allows the application to connect this filter to its parent XMLReader (or another filter). Public XmlReader getParent () Gets the parent XMLReader. This method allows the application to query the parent XMLReader (or another filter). It is best not to do anything directly in the parent XMLReader: let all events processed by this filter. We don't need to implement the org.xml.sax.xmlfilter interface, provide an org.xml.sax.Helper.xmlfilterImpl class in the SAX API, which not only implements the org.xml.sax.xmlFilter interface and other four Core processor interface, we only need to inherit it to complete our filters. It is easier for people who have just started using XMLFilterImpl, you only need to remember:</p> <p>In the XMLFilterImpl class in your inheritance, the event processor of the volume is used for the filtered event stream. Those event processing methods implemented in the XMLFilterIMPL class, such as StartDocument (), startElement (), and characters (), etc. are places where this filter implements its own function. This class implemented by inheriting XMLFilterImpl is molded into a variety of processors (itself implements four processor interfaces) in its parent XMLReader. This step is automatically executed when you call your own filter's Parse () method to start parsing the document (see SAX Source Code). If you don't use a parameter-using constructor to create an XMLFilter object, be sure to use the setParent (XmlReader Parent) method to connect its parent XMLReader. If multiple filters are used, the execution order is from the father to the final filter. However, the parsing begins to call the last filter PARSE () method. Let us combine existing examples to demonstrate the role of the filter org.xml.sax.xmlfilter. We have to filter out <Technology Books> This element in this filter, and finally the event stream is still processed by the MyDefaultHandler implemented by the upper side. The source code is as follows myfilter.java: postage com.javausr.saxexample;</p> <p>Import org.xml.sax. *;</p> <p>Import org.xml.sax.helpers. *; import java.io. *;</p> <p>Public class myfilter extends XMLFilterImpl {</p> <p>PRIVATE STRING CURRENTELEMENT;</p> <p>Public myfilter (XmlReader Parent) {</p> <p>Super (PARENT);</p> <p>}</p> <p>/ **</p> <p>* Filter out the elements <Technology Books></p> <p>** /</p> <p>Public void StartElement (String Namespaceuri, String Localname,</p> <p>String Fullname, Attributes Attributes)</p> <p>Throws saxception {</p> <p>Currentelement = localname;</p> <p>IF (! localname.equals) {</p> <p>Super.StartElement (Namespaceuri, localname, fullname, attributes);</p> <p>}</p> <p>}</p> <p>/ **</p> <p>* Filter out Elements <Technology Books> End Event</p> <p>** /</p> <p>Public void endelement (String Namespaceuri, String Localname, String</p> <p>Fullname)</p> <p>Throws saxception {</p> <p>IF (! localname.equals) {</p> <p>Super.Endelement (Namespaceuri, localname, fullname);</p> <p>}</p> <p>}</p> <p>/ **</p> <p>* Filter out the content of the element <Technology Book></p> <p>** /</p> <p>Public void characters (char [] buffer, int start, int ingth</p> <p>Throws saxception {</p> <p>IF (! Currentelement.equals) {</p> <p>Super.Characters (Buffer, Start, Length);</p> <p>}</p> <p>}</p> <p>} Only we have to modify mysaxapp.java, and the modified code is as follows: package com.javausr.saxexample;</p> <p>Import org.xml.sax.xmlreader;</p> <p>Import org.xml.sax.helpers.xmlreaderfactory;</p> <p>Import org.xml.sax.helpers.defaulthandler;</p> <p>// introduce XMLFilter</p> <p>Import org.xml.sax.xmlfilter;</p> <p>Import org.xml.sax.saxexception;</p> <p>Import java.io.ioException;</p> <p>Public class mysaxapp {</p> <p>Public static void main (String [] args) {</p> <p>IF (args.length! = 1) {</p> <p>System.out.println ("Enter: Java MySaxApp");</p> <p>System.exit (0);</p> <p>}</p> <p>Try {</p> <p>// Initialize Reader</p> <p>XmlReader Reader = XmlReaderFactory.createxmlReader</p> <p>("Org.apache.xerces.Parsers.saxparser");</p> <p>// Initializing the filter</p> <p>XMLFilter myfilter = new myfilter (reader);</p> <p>// Create an instance of defaulthandler</p> <p>DEFAULTHANDLER DefaultHandler = New MyDefaultHandler ();</p> <p>/ / Set ContentHandler for the filtered event stream</p> <p>MyFilter.SetContentHandler (DefaultHandler); // Set ErrorHandler for the filtered event stream</p> <p>Myfilter.setdler (DefaultHandler);</p> <p>/ / Start parsing the document, pay attention to use the parsing method in MyFilter</p> <p>myfilter.parse (Args [0]);</p> <p>} catch (ioexception e) {</p> <p>System.out.println ("When you read a document:" E.getMessage ());</p> <p>} catch (saxException e) {</p> <p>System.out.println ("Resolving the document:" E.GetMessage ());</p> <p>}</p> <p>}</p> <p>} This is the last execution result, we can find that all events related to <Technology Books> have been filtered off. Look at the results seriously, you must feel weird, why <technology books> Child elements of the element still exist. Keep in mind that SAX is parsing XML documents into event streams, all of which are not filtered. This is the maximum difference between SAX and DOM. The document in the DOM is parsed to a tree model. If you delete an element, then this element and its child elements will be deleted, which is characterized by the tree model. D: / SAX / CLASSES> Java com.javausr.saxexample.mysaxapp d: /book.xml</p> <p>******* Start parsing documents *******</p> <p>Element: [My Books] begins to resolve!</p> <p>Element: [Book] Start analysis!</p> <p>Element: [Title] Start analysis!</p> <p>The content is: Java 2 programming detailed</p> <p>Element: [book name] parsing end!</p> <p>Element: [Price] Start analysis!</p> <p>Property Name: Money unit attribute value: RMB</p> <p>The content is: 150</p> <p>Element: [Price] End!</p> <p>Element: [Purchase Date] Start resolution!</p> <p>The content is: 2000, 1, 24</p> <p>Element: [Buy Date] End!</p> <p>Element: [Book] End!</p> <p>Prefix mapping: Book Start! Its URI is: http://javausr.com</p> <p>Element: [Book: Literature Book] Start analysis!</p> <p>Element: [Book: Literature Book] End!</p> <p>Prefix mapping: Book ends!</p> <p>Element: [History Book] Start analysis!</p> <p>Element: [Historical Book] End!</p> <p>Element: [My Books] End!</p> <p>******* Resolution Document End ******* Some worthless problems are the problem of elementality, in the SAX API definition, element content can be in one event (by the character processing) Returns, you can also return in multiple events so that we should consider that all content data cannot be obtained at a time. The general solution is to define a StringBuffer to save content data, empty this StringBuffer at the end of the element or the new element, so that new content data can be saved. Please refer to the corresponding source code above. There is also a must not be read from the Returned Character array range from the Returned Character array, which is also known from the CHARACTERS (CHAR [] ch, int start, intling) method. Another worthless question is that the order in the attributes attribute list returned in the StarTelement () method is not specified, and it is different in different SAX implementations. So we don't want to make a certain order when writing a program. SAX and DOM comparison Through the above introduction I think everyone has a basic understanding of SAX. Each programmer who has developed XML knows that DOM, why do we need SAX after you have a powerful document object model with a DOM? This is to be analyzed from them. The DOM parser is done by parsing the XML document into a tree model and puts it into memory, and the operation of the document is completed on this tree model. This document tree in memory will be a few times more than the actual size of the document. The advantage of this is that the structure is cleared, the operation is convenient, and the trouble brought is extremely systematic resources. And SAX just overcomes the shortcomings of the DOM. The processing of the SAX parser is to pass through the entire document, generate an event according to the content of the document, and the processing of these events is handled by the event processor. SAX does not need to save the entire document in memory, which is obvious for the savings of system resources. This will use SAX in some cases that need to handle large XML documents and performance requirements. The following table lists SAX and DOMs in some aspects of control: SAXDOM sequentially read documents and generates the corresponding events, can handle any size XML documents to create a document tree in memory, not suitable for processing large XML documents. You can only resolve the document in order, and you don't support the free access to the document. Any part of the document tree can be accessed at will, without any number of restrictions. You can only read the XML document content without modifying the document tree at will, thereby modifying the XML document. Development is more complicated, you need yourself to implement an event processor. Easy to understand, easy to develop. More flexible, you can create your own XML object model with SAX. The document tree has been created on the ground. Through the analysis of SAX and DOM, they have their own different application areas:</p> <p>SAX is suitable for processing the following questions: Treatment of large documents. Only some of the contents of the document are required, or only a specific information is obtained from the document. When you want to create your own object model. The DOM is suitable for processing the following questions:</p> <p>You need to modify the document that needs to be randomly accessed, such as the XSLT parser. Introduction to SAX, I will tell you here, I hope to help everyone :), most of this article comes from http://www.megginson.com/sax/ and SAX API (although SAX With your new website http://sax.sourceforge.net/, I have never successful visits!), Thanks to David Megginson and other SAX developers to provide us with such a good stuff. If you have errors and improper places, please refer to.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-122987.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="122987" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.041</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'dzwin_2FWKJDDngYlIKFIwsRrtyinYS94bNeko_2FQnbQI9Q85oZd7COp3YrQ6JPuXDxxkGbQu0OfqkX_2B72Z'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>