Java and XML joint programming in SAX

Source: Internet
Author: User

SAX concepts
SAX is the abbreviation of Simple API for XML. It is not a standard officially proposed by W3C. It can be said that it is a "folk" fact standard. In fact, it is a product of community discussions. Even so, there is no less DOM than the application of SAX in XML, and almost all XML parser will support it.
Compared with DOM, SAX is a lightweight method. We know that when processing the DOM, We need to read the entire XML document, and then create a DOM tree in the memory to generate each Node object on the DOM tree. When the document is small, this will not cause any problems, but once the document is large, it will become quite time-consuming and laborious to process the DOM. In particular, its demand for memory will also multiply, making it uneconomical to use DOM in some applications (such as in the applet ). At this time, a better alternative solution is SAX.
SAX is conceptually different from DOM. First of all, unlike the DOM document driver, it is event-driven, that is, it does not need to read the entire document, and the document reading process is the parsing process of SAX. Event-driven is a program running method based on the callback mechanism. (If you have a clear understanding of the new proxy event model in Java, this mechanism will be easily understood)
When XMLReader accepts XML documents, the XML documents are parsed during reading. That is to say, the process of reading the documents and the process of parsing are performed at the same time, which is very different from the DOM. Before parsing, You need to register a ContentHandler with XMLReader, which is equivalent to an event listener. Many methods are defined in ContentHandler, such as startDocument (), which is customized During the parsing process, something that should be handled at the beginning of the document. When XMLReader reads the appropriate content, it will throw the corresponding event and delegate the event processing permission to ContentHandler, and call the corresponding method to respond.
In this case, it may not be easy to understand. Don't worry. The following example will help you understand the parsing process of SAX. Let's take a look at this simple XML file:
<POEM>
<AUTHOR> Ogden Nash </AUTHOR>
<TITLE> Fleas </TITLE>
<LINE> Adam </LINE>
</POEM>
When XMLReader reads the <POEM> tag, it calls the ContentHandler. startElement () method and passes the tag name POEM as a parameter. In the startElement () method you implement, You need to perform corresponding actions to deal with what should be done when <POEM> appears. Each event is thrown out sequentially along with the parsing process (that is, the process of reading the document), and the corresponding methods are called sequentially. Finally, when the parsing is complete, after the methods are called, the document processing is completed. The following table lists the methods called in sequence when parsing the preceding XML file:
ContentHandler is actually an interface. when processing a specific XML file, you need to create a ContentHandler class for it to process specific events. It can be said that, this is actually the core of processing XML files by SAX. Let's take a look at some of the methods defined here:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.