IBM's X-usax Tutorial sax tutorial

Source: Internet
Author: User
Tags object model web services xml parser java web

Source: IBM Tutorial Download

SAX is what.

The standard used to read and manipulate XML files is the Document Object model (Doc object model,dom). Unfortunately, the DOM method involves reading the entire file and storing the file in a tree structure, which can be inefficient, slow, and resource-intensive.

An alternative technique is simple APIs for XML, or SAX. SAX allows you to process a document while it is being read, eliminating the need to wait for the entire document to be stored before taking action.

SAX was developed by members of the Xml-dev mailing 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 the use of XML-in other words, the overhead and concept jumps that are not involved in DOM.

The outcome of the project is an event-based API. The parser sends an event to an event handler, such as the start of the element and the end of the element, while the event handler processes the information. The application itself is then able to process the data. The original documents remain intact, but SAX provides the means to manipulate the data, which can then be introduced into another process or document.

SAX does not have an official standard body; it is not maintained by the World Wide Web Consortium (Wide) or any other official agency, but it is the de facto standard of the XML community.

Tools

The examples in this tutorial (you should experiment with 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: The XML file is just a text file. To create and read them, all you need is a text editor. JAVATM 2 SDK , Standard Edition 1.4.x version: SAX support is built into the latest version of Java technology (available from http://java.sun.com/j2se/1.4.2/download.html), so you don't need to install any separate classes. If you are using older versions of Java, such as Java 1.3.x, you also need an XML parser, such as the Xerces-java of the Apache project (available from http://xml.apache.org/xerces2-j/ index.html), or Sun's Java API for XML parsing (JAXP), which is part of the Java Web Services development package (available from http://java.sun.com/webservices/downloads/ Webservicespack.html obtained). You can also download the official version from SourceForge (available from http://sourceforge.net/project/showfiles.php?group_id=29449). Other languages: if you want to adapt these examples, the SAX implementation can also be used in other programming languages. You can find information about C, C + +, Visual Basic, Perl, and Python implementations in Http://www.saxproject.org/?selected=langs.

SAX How the process works

SAX handles them while reading the XML stream, much like the previous auto ticker tape (ticker tape). Consider the following XML snippet:

     
     
     
     
     
     
      
      
     
     
 
     
     
      
      UNIX
     
     
     
     
 
     
     
      
      Color
     
     
     
     
    
    

The SAX processor that parses this snippet typically produces the following event:

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 and respond to these events.

SAX processing involves the following steps: Create an event handler. Create a SAX parser. Assigns an event handler to the parser. Parses the document and sends each event to the event handler.

the pros and cons of event-based processing

The advantages of this processing are very similar to the advantages of streaming media. The analysis can begin immediately, rather than waiting for all data to be processed. Also, because the application examines data only when it is being read, it does not need to store the data in memory. This is a huge advantage for large documents. In fact, the application doesn't even have to parse the entire document; it can stop parsing when a condition is met. In general, SAX is much faster than its surrogate DOM.

On the other hand, because applications do not store data in any way, it is not possible to use SAX to change data or to move backward through the data stream.

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.