The SAX parsing model of XML

Source: Internet
Author: User

DOM parsing will map the entire XML file into the tree structure in document, and when it comes to larger files, it takes up a lot of memory and looks very slow.

Sax is the solution for this scenario, and the SAX parser parses from the beginning of the XML file and, depending on the event handler already defined, determines whether the currently parsed part is necessary to record and store

Import Java.io.file;import Javax.xml.parsers.saxparser;import javax.xml.parsers.SAXParserFactory;      public class Saxparser{public static void Main () {file xmlfile = new File ("E:\\article.xml");      Saxparserfactoryfactory = Saxparserfactory.newinstance ();             try{Saxparserparser = Factory.newsaxparser ();      Parser.parse (Xmlfile,newmysaxhandler ());      }catch (Exception e) {e.printstacktrace (); }}} import Org.xml.sax.attributes;import Org.xml.sax.saxexception;import org.xml.sax.helpers.defaulthandler;public      Class Mysaxhandler extends defaulthandler{static DateFormat Formater = Newsimpledateformat ("Yyyy-mm-dd"); Private String content; Overwrite the Charaters method in DefaultHandler, receive notification of character data in the element, perform this operation after the Startelement or EndElement method is executed @Override publicvoid charater      S (char[] ch, int start, int length) throws saxexception{content = NewString (ch,start,length); }//triggers @Override publicvoid endElement when parsing to the end tag of an element (String urI,string LocalName, String qName) throws saxexception{if ("title". Equals (QName)) System.out.println ("title:"      + content);      else if ("Author". Equals (QName)) System.out.println ("" + content);      else if ("email". Equals (QName)) System.out.println ("E-mail:" + content);      else if ("date". Equals (QName)) System.out.println ("Day:" + content); }//triggers when parsing to the start tag of an element @Override publicvoid startelement (String uri,string localname, String qName attitudesattribute s) throws saxexception{if ("article". Equals (QName)) {System.out.println ("belongs to Category:" + attributes.getval             UE ("category")); }      }}

DefaultHandler class

When parsing XML data, it is necessary to create a listener object for parsing, which can generally be implemented by inheriting the DefaultHandler class.

void characters (char[] ch, int start, int length)

Receives notification of character data in an element.

void Enddocument ()

Receives notification of the end of the document.

void EndElement (String uri, String localname,string qName)

Receives notification of the end of the element.

void endprefixmapping (String prefix)

Receives notification of the end of a namespace mapping.

void error (Saxparseexception e)

Receives a recoverable parser error notification.

void FatalError (Saxparseexception e)

Reports a serious XML parsing error.

void Ignorablewhitespace (char[] ch, int start,int length)

A notification that can ignore whitespace in the content of a receive element.

void Notationdecl (string name, Stringpublicid, String systemid)

Receive notification of comment declarations.

void ProcessingInstruction (String target,string data)

Receive notification of processing instructions.

InputSource resolveentity (String publicid,string systemid)

Resolves an external entity.

void Setdocumentlocator (Locator Locator)

The Locator object that receives the document event.

void Skippedentity (String name)

Receive notifications for skipped entities.

void Startdocument ()

Receives notification of the beginning of a document.

void Startelement (String uri, Stringlocalname, String qName, Attributes Attributes)

The notification that the receive element starts.

Uri-the namespace URI, or an empty string if the element does not have any namespace URIs, or if no namespace processing is being performed.

LocalName-The local name (without a prefix), or an empty string if no namespace processing is being performed.

QName-The qualified name (with a prefix), or an empty string if the qualified name is not available.

Attributes-attributes attached to the element. If there is no property, it will be an empty Attributes object.

void startprefixmapping (String prefix, Stringuri)

Receives a notification that a namespace mapping begins.

void Unparsedentitydecl (string name, Stringpublicid, String systemid, String notationname)

Receive notifications for unresolved entity declarations.

void Warning (saxparseexception e)

Receives a notification of a parser warning.

Attributes

int GetIndex (String qName)

The index of the property is found through the XML qualified (prefix) name.

int GetIndex (string uri, String localname)

The index of the property is found by the name of the namespace.

int GetLength ()

Returns the number of properties in this list.

String getlocalname (int index)

Finds the local name of the property by index.

String getqname (int index)

The XML qualified (prefix) name of the lookup property by index.

String getType (int index)

The type of the property to find by index.

String GetType (String qName)

The type of the property is found through the XML qualified (prefix) name.

String GetType (String uri, String localname)

Finds the type of the property based on the name of the namespace.

String geturi (int index)

The namespace URI of the lookup property by index.

String getValue (int index)

The value of the Lookup property by index.

String GetValue (String qName)

Finds the value of an attribute through an XML qualified (prefix) name.

String GetValue (String uri, String localname)

Finds the value of a property based on the name of the namespace.

The SAX parsing model of XML

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.