XML parsing (sax detailed)

Source: Internet
Author: User

SAX (Simple API for XML XML-oriented easy APIs) is also a program interface.
The DOM can be parsed randomly, first loading the XML document into memory, and then accessing the DOM tree in memory in random ways. Sax is parsed sequentially, no need to load the entire XML into memory, once each element, we have no way to access him, so sax occupies less memory, more efficient. Parsing is usually done using sax parsing instead of DOM.

Sax is parsed in the form of callbacks. SAX uses the Observer pattern (similar to events in the GUI). A sax interface is called an event-driven interface.

Import Java.io.File;
Import java.io.IOException;

Import javax.xml.parsers.ParserConfigurationException;
Import Javax.xml.parsers.SAXParser;
Import Javax.xml.parsers.SAXParserFactory;

Import org.xml.sax.Attributes;
Import Org.xml.sax.HandlerBase;
Import org.xml.sax.SAXException;
Import Org.xml.sax.helpers.DefaultHandler;

public class SaxTest1
{
public static void Main (string[] args) throws Parserconfigurationexception, Saxexception, IOException
{
Get a parse factory instance
SAXParserFactory factory = Saxparserfactory.newinstance ();
Get a SAX parser
SAXParser parser = Factory.newsaxparser ();
To parse
Parser.parse (New File ("Student.xml"), New Myhander ());

}
}
Class Myhander extends DefaultHandler
{
@Override
public void Startdocument () throws Saxexception
{
SYSTEM.OUT.PRINTLN ("parse Begin");
}
@Override
public void Enddocument () throws Saxexception
{
System.out.println ("Parse finished");
}

@Override
public void Startelement (string uri, String localname, String qName,
Attributes Attributes) throws Saxexception
{
System.out.println ("start element");
}
@Override
public void EndElement (string uri, String localname, String qName)
Throws Saxexception
{
System.out.println ("finished element");
}
}

XML Schema Summary
Simple elements: Refers to only text content, no child elements, and no attributes.
Format: <xs:element name= "XX" type= "xs:string"/>

<xs:element name= "Age" type= "xs:string" >

Properties: All attributes of an element are defined as simple types, and only complex elements can have properties.
Format <xs:attribute name= "xx" type= "yyy"/>

All attributes are optional by default and can be explicitly indicated by the USE keyword as optional or necessary.
<xs:attribute name= "lang" type= "xs:string" use= "optional"/>
We can use default or fixed as a simple type (simple element, attribute)
Specify a default value or a fixed value.
<xs:element name= "Color" type= "xs:string" default= "Red"/>



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.