Java SAX Parser

Source: Internet
Author: User
Tags throwable xml parser

SAX is a abbreviation and means "simple API for XML". A Java SAX XML parser is a stream oriented XML parser. It works by iterating through the XML and call certain methods on a "listener" object when it meets certain structural Elemen TS of the XML. For instance, it would call the listener object for the following "events":

- startdocument- startelement- characters- comments- processing instructions - endElement-enddocument

This list was probably not complete, but it was long enough to give what's it works. Let's move on to see how do you create and use a Java SAX Parser.

SAXParserFactory factory = saxparserfactory.newinstance (); Try {    inputstream    xmlinput  new fileinputstream ("Thefile.xml");    SAXParser      = factory.newsaxparser ();    DefaultHandler handler   new  Saxhandler ();     Catch (Throwable err) {    err.printstacktrace ();}

When to call the  saxparser.parse ()  method The SAX parser starts the XML processing. the  xmlinput  inputstream  passed as parameter to the  Parse () The  method is where the XML was read from. Notice the  saxhandler  instance being created, and passed as parameter to the  Parse () /code> method. the  saxhandler  class is a subclass of the class  Org.xml.sax.helpers.DefaultHandler . the  DefaultHandler  class comes with the JDK.

While processing the XML " SAXParser the calls methods DefaultHandler in the subclass" here, the "instance corresponding to" what SaxHandler th E parser finds in the XML file. To react to those method calls your override the corresponding methods in the DefaultHandler subclass. Here are an example:

 Public classSaxhandlerextendsDefaultHandler { Public voidStartdocument ()throwssaxexception {} Public voidEnddocument ()throwssaxexception {} Public voidstartelement (String uri, String localname, String qName, Attributes Attributes)throwssaxexception {} Public voidendElement (String uri, String localname, string qName)throwssaxexception {} Public voidCharacters (CharCh[],intStartintlength)throwssaxexception {} Public voidIgnorablewhitespace (CharCh[],intStartintlength)throwssaxexception {}}

It is the responsibility of the DefaultHandler subclass to extract any necessary information from the XML via these methods. If you need to build a object graph based on a XML file, you'll have a to build that object graph inside the DefaultHandler SUBCL The.

ImportJava.io.FileInputStream;ImportJava.io.InputStream;ImportJavax.xml.parsers.SAXParser;Importjavax.xml.parsers.SAXParserFactory;Importorg.xml.sax.Attributes;Importorg.xml.sax.SAXException;ImportOrg.xml.sax.helpers.DefaultHandler; Public classTestsaxparser { Public Static voidTest () {SAXParserFactory factory=saxparserfactory.newinstance (); Try{InputStream Xmlinput=NewFileInputStream ("Thefile.xml"); SAXParser SAXParser=Factory.newsaxparser (); DefaultHandler Handler=NewSaxhandler ();        Saxparser.parse (Xmlinput, handler); } Catch(Throwable err) {err.printstacktrace (); }    }     Public Static voidMain (string[] args) {testsaxparser.test (); }}classSaxhandlerextendsDefaultHandler { Public voidStartdocument ()throwssaxexception {} Public voidEnddocument ()throwssaxexception {} Public voidstartelement (String uri, String localname, String qName, Attributes Attributes)throwssaxexception {System.out.println (URI+ ":" + LocalName + ":" +qName); }     Public voidendElement (String uri, String localname, string qName)throwssaxexception {System.out.println (URI+ ":" + LocalName + ":" +qName); }     Public voidCharacters (CharCh[],intStartintlength)throwssaxexception {} Public voidIgnorablewhitespace (CharCh[],intStartintlength)throwssaxexception {}}

Java SAX Parser

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.