Source: IBM Tutorial Download
What is SAX?
The standard for reading and operating the XML file is the document object model (Document Object Model, DOM). Unfortunately, the DOM method involves reading the entire file and stores the file in a tree structure, which may be inefficient, slow, and consume resource.
An alternative technology is Simple API for XML, or called SAX. SAX allows you to handle it when you read the document, so you don't have to wait for the entire document to be stored.
SAX is developed by members of the XML-DEV mail list, and the corresponding Java version is now a SourceForge project (see Resources). The purpose of the project is to provide a more natural means for XML's use - in other words, that is, the overhead and conceptual jumps necessary to DOM.
The results of the project are an event-based API. The parser sends events to an event handler, such as elements start and elements, and the event processor processes the information. The application itself can then process the data. The original document still keeps intact, but SAX provides means of operational data, and the data can be introduced into another process or document.
SAX has no official standards; it is not maintained by Wide Web Consortium, W3C) or any other official agency, but it is an XML community in fact standard.
tool
Examples in this tutorial (you should test them) need to install the following tools and make sure they work correctly. Running these examples is not necessary to understand this tutorial.
Text Editor: XML file is just a text file. To create and read them, what you need is a text editor.
JavaTM 2 SDK, Standard Edition 1.4.x version: SAX Support has been built into the latest version of Java technology (available from http://java.sun.com/j2se/1.4.2/download.html), so you don't Any separate class is required to install. If you are using an older version of Java, such as Java 1.3.x, you also need an XML parser, such as Xerces-Java, an Apache project (available from http://xml.apache.org/xerces2-j/index.html Get it), or Sun's Java API for XML Parsing (JAX), which is part of the Java Web service development package (available from http://java.sun.com/webservices/downloads/webservicespack.html). You can also download official version from SourceForge (available from http://sourceforge.net/project/showfiles.php?group_id=29449).
Other languages: If you want to adapt to these examples, SAX implementations can also be used in other programming languages. You can find information about C, C , Visual Basic, Perl, and Python.
How is SAX processing work?
)
SAX processes them while reading XML streams, which is very similar to the previous automatic receipt machine paper (Ticker Tape). Consider the following XML code snippet:
Unix
Color
Analysis of the SAX processor of this code snippet will be the following events: start document
START Element (Samples)
Characters (White Space)
START Element (Server)
Characters (UNIX)
End Element (Server)
Characters (White Space)
START Element (Monitor)
Characters (Color)
End Element (Monitor)
Characters (White Space)
End Element (Samples)
The SAX API allows developers to capture these events and respond to them.
SAX processing involves the following steps:
Create an event handler.
Create a SAX parser.
Assign an event handler to a parser.
Analyze the document while sending each event to the event handler.
Advantages and disadvantages of event-based processing
The advantage of this process is very similar to the advantages of streaming. The analysis can start immediately, not waiting for all the data being processed. Moreover, since the application is only checked when data is read, it is not necessary to store the data in memory. This is a huge advantage for large documents. In fact, the application does not even have to resolve the entire document; it can stop parsing when a condition is satisfied. In general, SAX is still much faster than its alternative DOM.
On the other hand, since the application does not store data in any way, it is impossible to change the data using SAX or in the data stream.
Sample file
This tutorial will show how to construct such an application, which uses SAX to record a group of users' answers, which are required to have an investigation with the experience of aliens.
Below is the questionnaire:
The answer will be stored in an XML file:
A
B
A
Di
B
C
A
A
Di
A
A
C
A
Di
C
Create an event handler
Before the application can use SAX to process an XML document, it must first create an event handler. SAX provides a class defaultHandler, which can extend this class.
To make the correct resolution using SAX, you need to conduct special calls for the process of handle, and these calls cannot be static. This means you need to instantiate the program object specifically, so a simple overview will be provided below to prevent you not to use the object.
When new objects are created, it looks for any class constructor to be executed. E.g:
Import org.xml.sax.helpers.defaulthandler;
Public Class SurveyReader Extends DefaultHandler
{
Public SurveyReader () {
System.out.println ("Object Created.");
}
Public void showevent (String name) {
System.out.println ("Hello," Name "!");
}
Public static void main (string args []) {
SurveyReader Reader = New SurveyReader ();
Reader.ShowEvent ("Nick");
}
}
When the MAIN method is executed, it creates a new instance of the SurveyReader class. This will cause the constructor to execute to output Object Created (and the following greetings). You can then use this object to perform the showEvent () method.
Advise the SAX driver
After ready for the event handler, the next step is to create a parser using the SAX driver, or XmlReader. You can use any of three ways to create a parser: directly call the driver
Allows the driver to be specified at runtime
Transfer the driver as the parameter of CreatexmlReader ()
If you know the class name of the SAX driver, you can call it directly. For example, if this class is (fiction) com.nc.xml.saxdriver, you can use the following code:
Try {
XmlReader XmlReader = new com.nc.xml.saxdriver ();
} catch (exception e) {
System.out.println ("CAN't create the Parser:" E.GetMessage ());
}
To create XMLReader directly.
You can also use the system properties to make the app more flexible. For example, you can specify the value of the class name as org.xml.sax.driver attribute when running the application.
Java-Dorg.xml.sax.driver = com.nc.xml.saxdriver SurveyReader
(Note that there should be no space behind -D.)
This makes this information to XmlReaderFactory, so you can use this code:
Try {
XmlReader XmlReader = XmlReaderFactory.createxmlReader ();
} catch (exception e) {
System.out.println ("CAN't create the Parser:" E.GetMessage ());
}
If you know that the driver name, you can also transfer it as a parameter of CreateXmlReader ().
Create a parser
This example uses a pair of class SaxParserFactory and SaxParser to create a parser, so you don't have to know the name of the driver itself.
First, declare XmlReader, XmlReader, then use SaXParserFactory to create a SaxParser. XmlReader is provided by SaXParser.
Import org.xml.sax.helpers.defaulthandler;
Import javax.xml.parsers.saxparser;
Import javax.xml.parsers.saxparserfactory;
Import org.xml.sax.xmlreader;
Public Class SurveyReader Extends DefaultHandler
{
Public SurveyReader () {
}
Public static void main (string args []) {
XmlReader XmlReader = NULL;
Try {
SAXPARSERFAACTORY SPFActory = saxparserfactory.newinstance ();
SAXPARSER SAXPARSER = SPFActory.newsaXParser ();
XmlReader = SAXPARSER.GETXMLREADER ();
} catch (exception e) {
System.err.Println (e);
System.exit (1);
}
}
Verification and non-verified parser
)
To do anything to use XML documents, you must read the information. Applications that do this work are called parsers. Two types of parsers are: non-verification and verification.
The non-verified parser is a parser suitable for a well-final document. It reads each information unit and adds it to the document - or handles events in the SAX application, regardless of the actual structure and content.
On the other hand, the verification parser checks the contents and structures of the XML document according to the defined syntax. Sometimes this syntax is in the form of document type definition, DTD, but it is more likely to be defined in the XML Schema document. In either case, the parser checks the document to ensure that each element and attribute are defined and contain the correct type of content. For example, you can specify that each ORDER has a Status (status). If you try to create a document without a syntax definition, the verification parser will prompt the error.
Documents that have been tested by the verification parser are considered a valid document.
Set the verification option
For this tutorial, you will not verify the results of the survey, so you can turn off the verification characteristics of any parser created by SaxParserFactory by setting the validating property:
...
Public static void main (string args []) {
XmlReader XmlReader = NULL;
Try {
SAXPARSERFAACTORY SPFactory =
SAXPARSERFACTORY.NEWINSTANCE ();
Spfactory.SetValidating (false);
SAXPARSER SAXPARSER =
Spfactory.newsaxparser ();
XmlReader = SAXPARSER.GETXMLREADER ();
} catch (exception e) {
System.err.Println (e);
System.exit (1);
}
}
...
Set content handler
The parser must send its event to a ContentHandler. For the sake of simplicity, SurveyReader will use as the primary application and content handler, so create a new instance and use XmlReader's setContentHandler () method to set it to contentHandler:
...
XmlReader = SAXPARSER.GETXMLREADER ();
XmlReader.SetContentHandler (New SurveyReader ());
} catch (exception e) {
...
Of course, this is the only option for the content handler. When this tutorials look at the serialized SAX flow later, you will see other options.
Analysis InputSource
)
To actually parse a file (or resolve anything else!) You need an InputSource. This SAX class wraps any of the data you want to process, so you don't have to care about where it comes.
Now you are ready to actually resolve files. Parse () method accepts the file, package it into the InputSource, and processes it, and sends each event to the ContentHander.
...
Import org.xml.sax.inputsource;
...
XmlReader = SAXPARSER.GETXMLREADER ();
XmlReader.SetContentHandler (New SurveyReader ()); InputSource Source = New InputSource ("Surveys.xml");
XmlReader.Parse (Source);
} catch (exception e) {
...
You can compile and run this program, but anything should not happen because the application has not defined any events.
Set Errorhandler
)
At this time, what should not happen, of course, the data you try to handle is always possible. In this case, there is a handler that handles errors and processing contents.
You can create an error handler like a content handler. Typically, you will create this handler as a separate instance of Errorhandler, but to simplify the examples here, we will include the error handles directly in SurveyResults. This dual use is achievable because the class expands DefaultHandler, and thus includes the implementation of the ContentHandler method and the Errorhandler method.
Setting new Errorhandler is just like setting up ContentHandler:
...
XmlReader.SetContentHandler (New SurveyReader ());
XmlReader.setdler (New SurveyReader ());
InputSource Source = New InputSource ("Surveys.xml");
...
Similarly, if you run an application at this time, it should still happen because the default implementation of each event does not do anything. In the next section, we will examine the addition of new implementations to handle events that occur during resolution.
StartDocument ()
)
Now, the document to be parsed is now set. Below you can start using the default implementation that actually do something when the handler receives the appropriate event, replacing the default implementation as the component of the DefaultHandler.
First use the startDocument () event to start the document, but don't do anything. This event will throw a SAXEXCEPTION like other SAX events:
...
Import org.xml.sax.saxexception;
Public Class SurveyReader Extends DefaultHandler
{
Public SurveyReader () {
}
Public void startdocument () throws saxexception {
System.out.Println ("Tallying Survey Results ...");
}
Public static void main (string args []) {
XmlReader XmlReader = NULL;
...
StartElement ()
Now check the actual data now. For each element, this example is retransmitted to the name of the StarTelement () event (see the Element List "screen snapshot below).
The parser actually passes multiple information for each element:
Limited name, or qname. This is actually a combination of namespace information (if any) and the actual name of the element. If there is, QNAME also includes a colon (:) - for example, Revised: Response.
Name Space URI. As discussed in the namespace, the actual namespace is some form of URI instead of being added to the alias of the element or attribute name. For example, http://www.neicholaschase.com/surveys/revised/ is not simple Revised :. Local name. This is the actual name of the element, such as questions. If the document does not provide namespace information, the parser may not determine which portion of the QNAME is LocalName.
Any attribute. The properties of the element are actually transmitted as a collection of objects, as can be seen from the next section.
First list the name of each element:
...
Public void StartElement (String Namespaceuri, String Localname,
String Qname, Attributes atts "throws saxception {
System.out.print ("Start Element:");
System.out.println (QNAME);
For (int att = 0; att String attname = atts.getqname (att); System.out.println (" attname ": " atts.getvalue (attname)); } } ... StarTelement (): Retrieval Properties ) StarTelement () event also provides access to element properties. These properties are incorporated in an Attributes data structure. You can retrieve an attribute value based on its location or attribute based name in an array. ... Public void StartElement (String Namespaceuri, String Localname, String Qname, Attributes atts "throws saxception { System.out.print ("Start Element:"); System.out.println (QNAME); For (int att = 0; att String attname = atts.getqname (att); System.out.println (" attname ": " atts.getvalue (attname)); } } ... overElement () You will find a lot of good reasons for paying attention to elements. For example, it may be a signal for processing element content. Here you will use it to properly print documents to some extent, and indent each layer element. Basic ideas are to increase the indentation value at the beginning of the new element, and reduce the moisture value at the end of the element: ... INT indent = 0; Public void startdocument () throws saxexception { System.out.Println ("Tallying Survey Results ..."); Indent = -4; } Public void printundent (int indeents) { For (int S = 0; s System.out.print (""); } } Public void StartElement (String Namespaceuri, String LocalName, String Qname, Attributes atts) throws saxexception { Indent = indent 4; PrintIndent (Indent); System.out.print ("Start Element:"); System.out.println (QNAME); For (int att = 0; att PrintIndent (Indenter); String attname = atts.getlocalname (att); System.out.println (" attname ": " atts.getvalue (attname)); } } Public void endelement (String Namespaceuri, String LocalName, String Qname) Throws saxception { PrintIndent (Indent); System.out.println ("End Element: localname); Indent = indent - 4; } ... CHARACTERS () ) Now you have obtained an element, you can continue to use characteristic data using character. Here, take a while, take a way to check the signature of this method: Public void characters (char [] ch, Int Start, INT Length) Throws SAXEXCEPTION Note that in this method, no information is anywhere indicating which element belongs to these characters. If you need this information, you must store it. This example adds several variables to store current elements and problem information. (It also deleted the large number of unrelated information displayed.) Please pay attention to two important matters here: Range: characters () event includes not only a string not only. It also includes initial and length information. In fact, the CH character array includes the entire document. The application must not attempt to feed the feed feed feed to the range of the range of the class () event. Frequency: The SAX specification does not require the processor to return characters in any particular manner, so returning a single text block in multiple parts. Before assume that you have all the contents of an element, you always make sure the endElement () event has occurred. Moreover, the processor may use ignorableWhitespace () to return a blank in an element. This is always in the verification parser. ... Public void printundent (int indeents) { For (int S = 0; s System.out.print (""); } } String thisquestion = ""; String thisElement = ""; Public void StartElement (String Namespaceuri, String Localname, String Qname, Attributes atts "throws saxception { IF (QName == "Response") { System.out.println ("User:");} else if (qname == "question") { thisquestion = atts.getValue ("Subject"); } ThisElement = QNAME; } Public void endelement (String Namespaceuri, String LocalName, String Qname) Throws saxception { THISQUESTION = "" ThisElement = ""; } Public void characters (char [] ch, int start, int ingth) Throws saxception { IF (thisElement == "question") { PrintINDENT (4); System.out.print (thisquestion ":"); System.out.println (New String (Ch, Start, Length)); } } ... Record an answer ) Now you have obtained data, you can continue to add actual records below. This is like constructing a string to analyze as simple as the survey is completed. ... String Appearance = NULL; String communication = null; String ship = NULL; String inside = NULL; String IMPLANT = NULL; Public void characters (char [] ch, Int Start, INT Length) Throws saxception { IF (thisElement == "question") { IF (thisquestion.equals ("APPEARANCE")) { Appearance = APPEARANCE New String (CH, Start, Length); } IF (thisquestion.equals ("Communication")) { Communication = Communication New String (CH, Start, Length); } IF (thisquestion.equals ("ship")) { SHIP = SHIP New String (CH, Start, Length); } IF (thisquestion.equals ("inside")) { INSIDE = INSIDE New String (CH, Start, Length); } IF (thisquestion.equals ("IMPLANT")) { IMPLANT = IMPLANT New String (CH, Start, Length); } } } ... Be sure, you can perform the only reason for this task in the characters () method, because the answer you want to find is only one character length. If the content you want to find has any length, you must collect the data obtained from each call, and then use the overElement () method at the end of the element. IgnorableWhitespace () is artificially prepared (not the program generated) XML document usually includes additional blank to make the document easier to read. Blank includes wraps, tabs, and spaces. In most cases, this blank is independent, and it should be ignored when processing data. All verification parsers and certain non-verified parsers are not passed to the content handler in Characters (), but passed in the IgnorableWhitespace () event. This is very convenient, because you can only focus on actual data. But what should I do if you really need a blank? In such a scenario, you can set an attribute on the element to indicate whether the processor does not ignore the blank character. This attribute is XML: Space, which is usually assumed to be default. (This means that the blank will be ignored unless the default behavior of the processor is retained. In order to tell the processor, don't ignore the blank, you can see this value set to Preserve, like this: Public void endelement String Namespaceuri, String Localname, String Qname) Throws SAXEXCEPTION EndDocument () Of course, once the resolution of the document is completed, you will need to print the final record as follows. This is also a good opportunity for any fragmented information that may occur during combination processing. ... IF (thisquestion.equals ("IMPLANT")) { IMPLANT = IMPLANT New String (CH, Start, Length); } } } Public int GetInstances (String All, String kice) { ... Return Total; } Public void enddocument () { System.out.println ("APPEARANCE OF THE AlIns:"); System.out.println ("A:" GetInstances (Appearance, "A")); System.out.println ("B:" GetInstances (Appearance, "B")); ... } Public static void main (string args []) { ... Processinginstruction () ) All of this is satisfactory, but sometimes you may want to directly include information about processing data. A good example is that you want to use the style sheet to handle documents, such as ... Other applications can get information in a similar approach. For example, the statistical sampling of the survey is undoubtedly not large enough to suit serious consideration. You can add a process of processing only for the application to specify a factor to enlarge the respondents of the respondent: ... This can be obtained by the Processinginstruction () event, and then divide it into target and data: ... Public void processinginstruction (String Target, String DATA): PUBLIC VOID PROCESSTRUCTION Throws saxception { System.out.println ("Target = (" Target ")") System.out.println ("DATA = (" Data ")") } ... Errorhandler Event Just like ContentHandler's events with predefined, ErrorHandler also has predefined events for handling errors. Since SurveyReader has been specified as an error handler and a content handler, you need to override the default implementation of those methods. You need to care about three events: Warning, Error and Fatalerror: ... Import org.xml.sax.saxparseException; Public Class SurveyReader Extends defaulthandler { Public SurveyReader () { } Public void error (SAXPARSEEXCEPTION E) { System.out.println ("Error Parsing The File:" E.GetMessage ()); } Public void Warning (SAXPARSEEXCEPTION E) { System.out.println ("Problem Parsing The File:" E.GetMessage (); } Public void Fatalerror (SAXPARSEEXCEPTION E) { System.out.println ("Error Parsing The File:" E.GetMessage ()); System.out.println ("Cannot CONTINUE."); System.exit (1); } ... Name space One of the SAX 20 editions is to add support for namespaces, namespaces allow developers to use different sources or information with different purposes without conflict. This usually appears in a production environment, data in SAX data streams in the production environment may come from many different sources. Namespace is a scope of concept, all of which need to be unique. For example, I used to work in such a office, my name is the same as one of the customers. If I am in the office, and the receptionist announces that "Nick, please pick up 1", "then everyone knows that she refers to me, because I am in the" Office "name space. Similarly, if she announced that "Nick is on line 1," everyone knows that she refers to that customer, because the caller is outside the office name space. On the other hand, if I have an office, and she makes the same announcement, confusion may be generated because the two possibilities exist. When the XML database is combined by a variety of sources, the same problem will also be generated, such as the survey information revised in the sample file will be described later in this tutorial. Create a namespace Page 2 (6 pages a total) Since the identifier of the namespace must be unique, use the Unified Resource Identifier (or URI) to specify them. For example, the default namespace of the sample data of this tutorial will use the XMLNs attribute to specify: XMLns = "http://www.neicholaschase.com/surveys/"> A ... Any nodes do not specify namespaces are in the default namespace http://www.necolaschase.com/surveys/. The actual URI itself does not represent any meaning. Information may not be in this address, it is important that it must be unique. Note that the default namespace and the huge difference between the fundamental no namespace are important. In this example, elements without namespace prefix are in the default namespace. Previously, when there is no default namespace, those elements are not in any namespace. This difference becomes important when you deal with namespace about attributes. You can also create subsequent namespaces and add elements or properties to them. Specify namespace Page 3 (6 pages a total) Other namespaces can also be specified for data. For example, by creating a revised namespace, you can add a second set of data, such as post-hypnosis, and not interfere with the original data. Namespace Contains alias is usually created on the root element of the document (but not necessarily this). This alias is used as an element or attribute as needed to specify the correct namespace as needed. Consider the following XML code snippet: XMLns: Revised = "http://www.nicholaschase.com/surveys/revised/"> A B A Di B Di A A Di A ... It uses namespaces and alias Revised to create a Question element. Remember Revised: Not a namespace, but an alias! The actual namespace is http://www.neicholaschase.com/surveys/revised/. Check the namespace As startlement () is briefly mentioned, the SAX version 2.0 adds to the functionality used to identify different namespaces. You can use these three new capabilities in a variety of ways, but first make sure there is only the original answer. Otherwise, the answer of Bob will be repeated. Because only when thisElement is "Question", you should perform the check before setting the variable. ... Public void startelement String Namespaceuri, String Localname, String Qname, Attributes atts Throws saxception { IF (Namespaceuri == "http://www.nicholaschase.com/surveys/") { IF (localname == "question") { thisquestion = atts.getValue ("Subject"); } } ThisElement = localname; } ... Note The application is actually checking the namespace URI (or as the URL as in this example) instead of checking the alias. Namespace about attributes Attributes may also belong to a particular namespace. For example, if the name of the problem has changed in the second round of investigation, you can add the second related property Revised: Subject, such as: XMLns: Revised = "http://www.nicholaschase.com/surveys/revised/"> A B A Di B Revised: Subject = "Looks"> D A A Di A ... A little strange place for attributes is that they will never be in the default namespace. Even if a default names have already been declared, there is no prefix attribute that it is considered to have no namespace. This means that Response is in http://www.nicholaschase.com/surveys/ namespace, but its attribute username is not in this namespace. This is just a definition of the XML recommended standard itself exists. The Attributes list has a method that allows you to determine the namespace of the properties. These methods are geturi () and getqname, which uses very similar elements themselves from QNAME and LocalName. Strangeness of the namespace The SAX parser handles local names and QNAME may be a bit strange. For example, the default Java parser does not report the value of the local name unless specifically opens the namespace processing. ... Try { SAXPARSERFAACTORY SPFActory = saxparserfactory.newinstance (); Spfactory.SetValidating (false); SPFActory.setfeature ("http://xml.org/sax/features/namespace-prefixes", True); SPFActory.setfeature ("http://xml.org/sax/features/namespaces", True); SAXPARSER SAXPARSER = SPFActory.newsaXParser (); ... If you have difficulty getting information, you can try these features of the parser. Serialized SAX stream The examples so far have been examined the basis of the use of data, but this is just simply examining what SAX can do. You can guide the data to another SAX process, convert, of course, can also guide a file. This section will show some of this option. The process of outputting the SAX event stream to the file is referred to as serialization. You can write a file yourself, but you should be more simple to use the Serializer object: Import org.apache.xalan.Serialize.Serializer; Import org.apache.xalan.serialize.serializerFactory; Import org.apache.xalan.templates.outputproperties; Import java.io.fileoutputstream; ... Public static void main (string args []) { XmlReader XmlReader = NULL; Try { SAXPARSERFAACTORY SPFActory = saxparserfactory.newinstance (); Spfactory.SetValidating (false); SAXPARSER SAXPARSER = SPFActory.newsaXParser (); XmlReader = SAXPARSER.GETXMLREADER (); Serializer Serializer = SerializerFactory.getSerializer OutputProperties.getDefaultMethodproperties ("XML"); Serializer.setOutputStream (New FileoutputStream ("Output.xml"); XmlReader.SetContentHandler Serializer.ascontentHandler () ); InputSource Source = New InputSource ("Surveys.xml"); XmlReader.Parse (Source); } catch (exception e) { System.err.Println (e); System.exit (1); } } ... Creating a Serializer object (OutputProperties acceptable value XML, Text, and HTML), and set it OutputStream. This stream can almost be an object of any flow type, such as a file (this is this) or System.out. You can set the Serializer to the parser's content handler so that when the parser resolves the file, it is the Serializer that receives the event. XMLFilters Page 2 (5 pages a total) Since SAX involves analyzing data (rather than after storage) when reading data (not after storage), there may be no way to change data before analyzing data. This is the problem to be solved by XMLFilter. Although they are newly introduced in SAX version 2.0, the intelligent programmer is already in use in version 1.0, they recognize that the SAX stream "link" can be reached in the final destination Effectively operate before. Basically, it works like this: Create an XMLFilter. This is usually a simple class. Create an instance of an XMLFilter and set its father to XmlReader that is usually responsible for resolving the file. Set the filter content handler to the usual content handler. analyse file. The filter is between the XMLReader and the content handler. Create a filter Now you need to create a filter to allow you to discard the original answer of the user, and use the revised answer viaway. To do this, you need to delete the original answer and change the namespace of the revision answer so that SurveyReader can choose them. This is implemented by creating a new class that extends XMLFilterIMPL. Investigate what happened here. When the startElement () event is excited, it will check the original namespace URI. If this is a revised element, the namespace will be changed to the default namespace. If this is not this element, the element name will actually be changed so that the original recording routine (in SurveyReader) does not identify it as a problem, so that the answer is not counted. The modified data will be passed to the Father (original XmlReader) StartElement () so that the content processor will be responsible for processing it. Import org.xml.sax.helpers.xmlfilterIMPL; Import org.xml.sax.xmlreader; Import org.xml.sax.saxexception; Import org.xml.sax.attribute; Public Class SurveyFilter Extends XMLFilterIMPL { Public SurveyFilter () { } Public SurveyFilter (XMLReader Parent) { Super (PARENT); } Public void startElement (String Uri, String Localname, String Qname, Attributes atts Throws SAXEXCEPTION { IF (URI == "http://www.nicholaschase.com/surveys/revised/") {uri = "http://www.nicholaschase.com/surveys/"; Qname = "quothes"; } else { LocalName = "reject"; } Super.StartElement (URI, LocalName, QNAME, ATTS); } } Call filter ) It is time to use the filter now. The first thing to do is to create a new instance and then specify the original XMLReader as a father. Next, set the contents of the filter and the error handler instead of setting the reader. Finally, the filter is used instead of XMLReader to resolve the file. Since XMLReader is designated as a filter, it still wants information. ... Public static void main (string args []) { XmlReader XmlReader = NULL; Try { SAXPARSERFAACTORY SPFactory = SAXPARSERFACTORY.NEWINSTANCE (); Spfactory.SetValidating (false); SAXPARSER SAXPARSER = Spfactory.newsaxparser (); XmlReader = SAXPARSER.GETXMLREADER (); SurveyFilter XMLFilter = New SurveyFilter (); XMLFilter.SetParent (XMLReader); XMLFilter.SetContentHandler (New SurveyReader ()); XMLFilter.setdler (New SurveyReader ()); InputSource Source = New InputSource ("Surveys.xml"); XMLFilter.Parse (SOURCE); } catch (exception e) { System.err.Println (e); System.exit (1); } } ... The depth of these properties is unlimited. In theory, you can create a long chain of a filter, and each filter calls the next filter. Use XMLFilter to convert data XMLFilters can also be used to easily quickly use XLST conversion data. The conversion itself exceeds the scope of this tutorial, but the following will be briefly examined how to apply it: Import javax.xml.transform.Stream.streamsource; Import javax.xml.transform.transformer; Import javax.xml.transform.transformerfactory; Import javax.xml.transform.sax.saxtransformerfactory; Import org.xml.sax.xmlfilter; ... Public static void main (string args []) { XmlReader XmlReader = NULL; Try { SAXPARSERFAACTORY SPFActory = saxparserfactory.newinstance (); Spfactory.SetValidating (false); SAXPARSER SAXPARSER = SPFActory.newsaXParser (); XmlReader = saxparser.getxmlreader (); transformerfactory tfactory = transformerfactory.newinstance (); SaxTransformerFactory SaxtFactory = (SaxtransformerFactory) TFactory; XMLFilter XMLFilter = SaxtFactory.newxmlFilter (New StreamSource ("surveys.xsl"); XMLFilter.SetParent (XMLReader); Serializer Serializer = SerializerFactory.getSerializer OutputProperties.getDefaultMethodproperties ("XML"); Serializer.SetOutputStream (System.out); XMLFilter.SetContentHandler Serializer.ascontentHandler ()); InputSource Source = New InputSource ("Surveys.xml"); XMLFilter.Parse (SOURCE); } catch (exception e) { System.err.Println (e); System.exit (1); } } ... First, you need to create a filter - but not from head creation, but create a filter specifically designed to perform conversion based on style sheets. Then, as you do when you do direct output files, create a Serializer to output the conversion result. Basically, the filter performs conversion, then hand over the event to XmlReader. However, the final destination is a serializer. to sum up ) Compared to document object models (DOM), SAX is a faster, lighter method for reading and operating XML data. SAX is an event-based processor that allows you to process elements, properties, and other data that appear in the original document. Due to such an architecture, SAX is a read-only system, but then does not prevent you from using data. This tutorial also examines the use of SAX to determine the namespace, and how to use XSL to output and convert data. This tutorial finally examined the filter, the filter allows you to link the operation.