Detailed Android using SAX to parse XML files _android

Source: Internet
Author: User
Tags cdata int size object model stub xml parser

There are many ways to parse XML, and it may be that Dom parsing is more familiar.

Dom (file object Model) Resolution: The parser reads the entire document and then constructs a tree structure that resides in memory, and then the code can manipulate the tree structure based on the DOM interface.

Advantages: The entire document read into memory, easy to operate: support modification, delete and reproduce the arrangement and other functions.

disadvantage: read the entire document into memory, leaving too many unwanted nodes, wasting memory and space.

Usage: Once you have read the document, you will need to work on the document multiple times and with sufficient hardware resources (memory, CPU).

To solve the problem of Dom parsing, there is a sax parsing. Its characteristics are:

Advantages: No implementation of the transfer of the entire document, less resource consumption. Especially in embedded environments, such as Android, it is highly recommended to use Sax parsing.

Disadvantage: Unlike DOM parsing, the document resides in memory for a long time, and the data is not persistent. If the data is not saved after the event, the data is lost.

Use occasion: The machine has the performance limit.

The Sax parsing XML document takes event-driven mode. What is event-driven mode? It converts an XML document into a series of events, which is determined by a separate event handler.

Event-driven processing patterns are primarily based on event sources and event handlers (or listeners). An object that can produce an event is called an event source, and an object that can respond to an event is called an event handler.

In the Sax interface, the event source is the XmlReader in the Org.xml.sax package, and he starts parsing the XML document through the parse () method and generates events based on the contents of the document. The event handler is the ContentHandler, Dtdhandler, ErrorHandler, and entityresolver four interfaces in the Org.xml.sax package. They handle events in which the event source generates different classes during the parsing process (where Dtdhandler is used when parsing the document DTD). The following table is described in detail:

In the above four interfaces, the most important thing is to contenthandler this interface, the following is a description of this interface method:

Sets a locator object public void Setdocumentlocator (Locator Locator) that can locate the location where the document content event occurred

//For handling document resolution start events public

void Startdocument () throws the Saxexception

//Process element start event, from which you can get the URI of the element's namespace, the element name, the property class table, and other information public

void Startelement ( String Namespacesuri, String LocalName, String qName, Attributes atts) throws saxexception

//Processing element end event, from parameter can get element's name namespace URI, element name information public

void EndElement (String Namespacesuri, String localname, String qName) throws Saxexception
   //handles the character content of the element, from which you can obtain content public

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

Here we will introduce the method in the next XmlReader.

Register processing XML Document resolution event ContentHandler public
void Setcontenthandler (ContentHandler handler)

//Start parsing an XML document
public void Parse (Inputsorce input) throws Saxexception

Steps for Sax to implement entity resolution:

Using Sax in Android is traceable, and you can easily find the tag in XML and get what you want by following these methods. The following steps are implemented:

(a) First step: Create a new factory class SAXParserFactory, the code is as follows:

SAXParserFactory factory = Saxparserfactory.newinstance ();

(ii) Step two: Let the factory class produce a Sax parsing class saxparser with the following code:

SAXParser parser = Factory.newsaxparser ();

(iii) Step three: A XmlReader instance is obtained from the Saxpsrser code as follows:

XMLReader reader = Parser.getxmlreader ();

(iv) The fourth step: to write their own handler registered to the XmlReader, the general most important is ContentHandler, the code is as follows:

RssHandler handler = new RssHandler ();
Reader.setcontenthandler (handler);

(v) Step fifth: After an XML document or resource becomes a Java-capable InputStream stream, parsing begins with the following code:

Parser.parse (IS);

In the above steps, the most important and most critical is the fourth step, handler implementation.

The following is an example of an RSS parsing to illustrate the implementation of handler:

We first see an RSS XML document for local resolution, and the new RSS document is as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <channel> <title>rss resolution Exercise </title> <descriptio N>hehehaha</description> <link>http://www.cnblogs.com/felix-hua/</link> <language> Zh-cn</language> <item> <title><! [cdata[headline]]></title> <link>http://mc.cz001.com.cn/images/menu/23_active.png</link> &LT;CA tegory>0</category> <description> Description of </description> &LT;PUBDATE&GT;2012-01-09&LT;/PUBD for detailed information ate> </item> <item> <title><! [cdata[News]]></title> <link>http://mc.cz001.com.cn/images/menu/23_active.png</link> <ca tegory>0</category> <description> Description of </description> &LT;PUBDATE&GT;2012-01-09&LT;/PUBD for detailed information ate> </item> <item> <title><! [Cdata[Home]]></title> <link>http://mc.cz001.com.cn/images/menu/23_active.png</link> <category>0</category> <description> Description of &L for detailed information t;/description> <pubDate>2012-01-09</pubDate> </item> <item> <title ><! [cdata[Financial]]></title> <link>http://mc.cz001.com.cn/images/menu/23_active.png</link> <ca tegory>0</category> <description> Description of </description> &LT;PUBDATE&GT;2012-01-09&LT;/PUBD for detailed information

 Ate> </item>

Once we've built it, we'll name it rssxml.xml and put it under the root of the project:

Then we can create two entity classes:

1, RssFeed, and the complete XML document corresponding;

2, RssItem, and the item label corresponding to the information.

So when parsing XML, we can put the parsed information into the entity class, and then directly manipulate the entity classes. The following code is given:

Rssfeed.java

Package com.sax.org.entity;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;

Import Java.util.Vector;
  public class RssFeed {private String title;
  private int itemcount;
  
  Private list<rssitem> itemlist;
  Public RssFeed () {itemlist = new vector<rssitem> (0); /** * is responsible for adding a rssitem to the RssFeed class * @param item * @return/public int AddItem (RssItem item) {ITEML
    Ist.add (item);
    itemcount++;
  return itemcount;
  Public RssItem getitem (int location) {return Itemlist.get (location);
  Public list<rssitem> Getallitems () {return itemlist; /** * The data required to generate a list from the RssFeed class * @return/Public list Getallitemforlistview () {list<map<string
    , object>> data = new arraylist<map<string,object>> ();
    int size = Itemlist.size (); for (int i=0; I<size i++) {hashmap<string, object> item = new hashmap<string, OBject> ();
      Item.put (Rssitem.title, Itemlist.get (i). GetTitle ());
      Item.put (Rssitem.pubdate, Itemlist.get (i). Getpubdate ());
    Data.add (item);
  } return data;
  Public String GetTitle () {return title;
  public void Settitle (String title) {this.title = title;
  public int GetItemCount () {return itemcount;
  The public void Setitemcount (int itemcount) {this.itemcount = ItemCount;
  Public list<rssitem> GetItemList () {return itemlist;
  public void Setitemlist (list<rssitem> itemlist) {this.itemlist = itemlist;

 }
  
}

Rssitem.java

 package com.sax.org.entity;
  public class RssItem {public static String title = "title";
  public static String pubdate = "pubdate";
  Public String title;
  public String description;
  public String link;
  public String category;
  Public String pubdate;
  Public RssItem () {} public String GetTitle () {return title;
  public void Settitle (String title) {this.title = title;
  Public String GetDescription () {return description;
  } public void SetDescription (String description) {this.description = description;
  Public String GetLink () {return link;
  public void Setlink (String link) {this.link = link;
  Public String GetCategory () {return category;
  } public void Setcategory (String category) {this.category = category;
  Public String Getpubdate () {return pubdate;
  } public void Setpubdate (String pubdate) {this.pubdate = pubdate; }
  
  
}

Here is the most important place to build your own contenthandler. Look at the following code:

Rsshandler.java

Package Com.sax.org.handler;
Import org.xml.sax.Attributes;
Import org.xml.sax.SAXException;

Import Org.xml.sax.helpers.DefaultHandler;
Import Com.sax.org.entity.RSSFeed;

Import Com.sax.org.entity.RSSItem;
  public class RssHandler extends defaulthandler{rssfeed rssfeed;
  RssItem RssItem;
  final int rss_title = 1;
  final int rss_link = 2;
  Final int rss_description = 3;
  Final int rss_category = 4;
  Final int rss_pubdate = 5;
  
  int currentstate = 0;
  Public RssHandler () {} public RssFeed Getfeed () {return rssfeed; @Override public void Startdocument () throws Saxexception {//TODO auto-generated method Stub rssfeed =
    New RssFeed ();
  RssItem = new RssItem (); @Override public void Enddocument () throws Saxexception {//TODO auto-generated method stub} @O Verride public void Startelement (string uri, String localname, String qName, Attributes Attributes) throws Saxexce ption {//TODO auto-generated method sTub if (localname.equals ("channel")) {currentstate = 0;
    Return
      } if (Localname.equals ("item")) {RssItem = new RssItem ();
    Return
      } if (Localname.equals ("title")) {currentstate = Rss_title;
    Return
      } if (Localname.equals ("description")) {currentstate = rss_description;
    Return
      } if (Localname.equals ("link")) {currentstate = Rss_link;
    Return
      } if (Localname.equals ("category")) {currentstate = rss_category;
    Return
      } if (Localname.equals ("pubdate")) {currentstate = rss_pubdate;
    Return
  } currentstate = 0; @Override public void EndElement (string uri, String localname, String qName) throws Saxexception {//T
      Odo auto-generated Method Stub if (Localname.equals ("item")) {Rssfeed.additem (RssItem);
    Return } @Override public void characters (char[] ch, int start, int length) throws Saxexception {// TODO auto-generated Method Stub String thestring = new String (ch, start, length);
      Switch (currentstate) {case RSS_TITLE:RssItem.setTitle (thestring);
      CurrentState = 0;
    Break
      Case RSS_DESCRIPTION:RssItem.setDescription (thestring);
      CurrentState = 0;
    Break
      Case RSS_LINK:RssItem.setLink (thestring);
      CurrentState = 0;
    Break
      Case RSS_PUBDATE:RssItem.setPubdate (thestring);
      CurrentState = 0;
    Break
      Case RSS_CATEGORY:RssItem.setCategory (thestring);
      CurrentState = 0;
    Break
    Default:return;

 }
  }
}

On the above code analysis, the implementation of a contenthandler generally take a few steps:

1, declare a class, inherit DefaultHandler. DefaultHandler is a base class in which a contenthandler is simply implemented. We just need to rewrite the inside method.

2, rewrite startdocument () and Enddocument (), the general resolution will be formally resolved some of the initial salary before the startdocument (), the finishing work put into enddocument () inside.

3, rewrite startelement (), the XML parser encountered in the XML inside the tag will call this function. It is often in this function that the localname are judged by the two of them and manipulate some data.

4. Rewrite the characters () method, which is a callback method. After the parser finishes startelement (), the node's content is parsed, and the parameter ch[] is the node's content. In this example we judge the contents of the current tag according to the CurrentState, and put it in the appropriate entity class.

5, rewrite EndElement () method, this method and Startelement () corresponding, parse a tag node, execute this method. To find another example, if you parse an item end, add Rssiiem to the RssFeed.

Finally, we implement an activity to show the results of the resolution:

 Package com.sax.org;
Import java.io.IOException;

Import Java.net.URL;
Import javax.xml.parsers.ParserConfigurationException;
Import Javax.xml.parsers.SAXParser;

Import Javax.xml.parsers.SAXParserFactory;
Import Org.xml.sax.InputSource;
Import org.xml.sax.SAXException;

Import Org.xml.sax.XMLReader;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.ListView;
Import Android.widget.SimpleAdapter;

Import Android.widget.TextView;
Import Com.sax.org.entity.RSSFeed;
Import Com.sax.org.entity.RSSItem;

Import Com.sax.org.handler.RSSHandler; The public class Saxreaderactivity extends activity {/** called the ' when ' is the ' The activity ' is a./Public String RSS
  URL = "Http://mc.cz001.com.cn/a/indexconfig/index.rss";

  public RssFeed feeds;
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    feed = Getfeed (Rssurl);
  Showlist (); } public RssFeed Getfeed (String rssurl{try {//Here we have implemented a local resolution, so we dropped this data from the network.
      URL url = new URL (rssurl);
      SAXParserFactory factory = Saxparserfactory.newinstance ();
      SAXParser parser = Factory.newsaxparser ();
      XMLReader reader = Parser.getxmlreader ();
      RssHandler handler = new RssHandler ();
      Reader.setcontenthandler (handler); InputSource is = new InputSource (This.getclassloader (). getResourceAsStream ("Rssxml.xml"))//Gets the local XML file Reader.parse (
      IS);
    return Handler.getfeed ();
    catch (Parserconfigurationexception e) {//TODO auto-generated catch block E.printstacktrace ();
    catch (Saxexception e) {//TODO auto-generated catch block E.printstacktrace ();
    catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
  return null;
    public void Showlist () {ListView Rsslistview = (ListView) Findviewbyid (r.id.rsslist);
    TextView Rsstitle = (TextView) Findviewbyid (r.id.rsstitle); if (Feed = = null) {Rsstitle.settext ("Access failed ...");
    Return } simpleadapter adapter = new Simpleadapter (this, Feed.getallitemforlistview (), Android. R.layout.simple_list_item_2, new string[] {rssitem.title, rssitem.pubdate}, new int[] {Android. R.id.text1, Android.
    R.ID.TEXT2});
  Rsslistview.setadapter (adapter);

 }
}

Show Run Results:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud-dwelling community.

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.