Android XML Data parsing (2)--sax parsing

Source: Internet
Author: User

(The following article basically copied the Guo Lin "first line of code")

In the resolution XML file for Android (1)--pull parsing we talked about the Pull method parsing the XML file. Today is another way to tell sax parsing XML files.

First, you should look at the code first.

One, Sax parsing reference code

Private voidParsexmlwithsax (String xmlData) {Try{ saxparserfactory factory=saxparserfactory.newinstance (); XMLReader XMLReader=Factory.newsaxparser (). Getxmlreader (); ContentHandler Handler=NewContentHandler (); //to set an instance of ContentHandler to XmlReaderXmlReader. Setcontenthandler        (handler); //Start Execution parsingXmlReader.Parse(NewInputSource (NewStringReader (xmlData)); }Catch(Exception e) {e.printstacktrace (); }}

Here we see that sax parsing looks a lot more concise than pull parsing.

Ii. Introduction of related Categories

1, SAXParserFactory

SAXParserFactory is similar to the Xmlpullparserfactory class. It is also a factory that provides saxparser instances.

The following is the definition of the SAXParserFactory class in the Android API.

The following are the methods defined in SAXParserFactory. The newinstance () and Newsaxparser () used in the code are used to produce a saxparserfactory instance and produce a SAXParser instance respectively.

2, XMLReader

XmlReader is an interface. Through its Setcontenthandler () method, you can set the processing handler of the parse event, and the parse () method can begin parsing.

An instance of XmlReader is obtained through the Getxmlreader () method of the SAXParser class.

3, ContentHandler

The ContentHandler class in the appeal code is a class that we write ourselves, inheriting from the DefaultHandler class. The DefaultHandler class is the default base class for SAX2 event handling.

It provides the default implementation of all callbacks in the four core sax processing classes. We can inherit the class and rewrite our processing logic in the corresponding method.

The following is a description of the DefaultHandler class in the Android API.

The specific code for the DefaultHandler class that we inherit is as follows:

 Public classContentHandlerextendsdefaulthandler{PrivateString NodeName; PrivateStringBuilder ID; PrivateStringBuilder name; PrivateStringBuilder version; //Initialize@Override Public voidStartdocument ()throwssaxexception{ID=NewStringBuilder (); Name=NewStringBuilder (); Version=NewStringBuilder (); } @Override Public voidstartelement (String uri,string localname, String qname,attributes Attributes)throwssaxexception{//record the current node nameNodeName =LocalName; } @Override Public voidCharacters (Char[] ch,intStart,intLengththrowssaxexception{//add content to which StringBuilder object according to the current node name        if("id". Equals (NodeName))        {id.append (ch,start,length); }Else if("Name". Equals (NodeName))        {name.append (ch,start,length); }Else if("Version". Equals (NodeName))        {version.append (ch,start,length); }} @Override Public voidendElement (String uri,string localname, string qName)throwssaxexception{if("App". Equals (LocalName) {LOG.D ("ContentHandler", "ID is" +id.tostring (). Trim ()); LOG.D ("ContentHandler", "name is" +name.tostring (). Trim ()); LOG.D ("ContentHandler", "version is" +version.tostring (). Trim ()); //Finally, I want to talk about StringBuilder emptying .Id.setlength (0); Name.setlength (0); Version.setlength (0); }} @Override Public voidEnddocumentthrowssaxexception{}}

You can see that the processing of XML data in the ContentHandler class is similar to pull parsing. So sax parsing is just wrapping up the parsing method, and the logic is clearer when the code is written.

In summary, using SAX to parse XML data we need to do the following steps:

1. Use the Newinstance () method in the SAXParserFactory class to get the SAXParserFactory class instance.

2. Get SAXParser instance by Newsaxparser () method of SAXParserFactory class instance

3. Get an instance of XmlReader by SAXParser instance Getxmlreader ()

4. XmlReader instance calls Setcontenthandler (ContentHandler ContentHandler) method sets the processing events required for parsing

5. Customize a class to inherit from DefaultHandler, overriding the methods we need (these methods are the logic we use to process XML data).

6. Finally call XmlReader's Parse () method to parse the data.

Android XML Data parsing (2)--sax parsing

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.