2011年5月16日月曜日

AndroidStudyMemo=Parsing XML from the Network

A large portion of data transmitted between network resources is stored in a structured

fashion in Extensible Markup Language (XML). In particular, RSS feeds are provided in a

standardized XML format, and many web services provide data using these feeds.

 

Android SDK provides a variety of XML utilities.We dabble with the XML Pull Parser

in Chapter 6,"Managing Application Resources."We also cover the various SAX and

DOM support available in Chapter 10,"Using Android Data and Storage APIs."

Parsing XML from the network is similar to parsing an XML resource file or a raw file

on the file system.Android provides a fast and efficient XML Pull Parser, which is a parser

of choice for networked applications.

 

The following code demonstrates how to use the XML Pull Parser to read an XML file

from flickr.com and extract specific data from within it.A TextView called status is assigned

before this block of code is executed and displays the status of the parsing operation.

              import java.net.URL;

              import org.xmlpull.v1.XmlPullParser;

              import org.xmlpull.v1.XmlPullParserFactory;

              // ...

              URL text = new URL(

                                          "http://api.flickr.com/services/feeds/photos_public.gne

                                          ??id=26648248@N04&lang=en-us&format=atom");

              XmlPullParserFactory parserCreator =

                            XmlPullParserFactory.newInstance();

             

              XmlPullParser parser = parserCreator.newPullParser();

              parser.setInput(text.openStream(), null);

             

              status.setText("Parsing...");

              int parserEvent = parser.getEventType();

              while (parserEvent != XmlPullParser.END_DOCUMENT) {

                            switch(parserEvent) {

                                          case XmlPullParser.START_TAG:

                                                        String tag = parser.getName();

                                                        if (tag.compareTo("link") == 0) {

                                                                      String relType =

                                                                                    parser.getAttributeValue(null, "rel");

                                                                      if (relType.compareTo("enclosure") == 0 ) {

                                                                                    String encType =

                                                                                                  parser.getAttributeValue(null, "type");

                                                                                    if (encType.startsWith("image/")) {

                                                                                                  String imageSrc =

                                                                                                                parser.getAttributeValue(null, "href");

                                                                                                  Log.i("Net",

                                                                                                                "image source = " + imageSrc);

                                                                                    }

                                                                      }

                                                        }

                                                        break;

                            }

                            parserEvent = parser.next();

              }

              status.setText("Done...");

 

After the URL is created, the next step is to retrieve an XmlPullParser instance from the

XmlPullParserFactory.A Pull Parser has a main method that returns the next event.The

events returned by a Pull Parser are similar to methods used in the implementation of a

SAX parser handler class. Instead, though, the code is handled iteratively.This method is

more efficient for mobile use.

 

In this example, the only event that we check for is the START_TAG event, signifying the

beginning of an XML tag.Attribute values are queried and compared.This example looks

specifically for image URLs within the XML from a flickr feed query.When found, a log

entry is made.

 

You can check for the following XML Pull Parser events:

START_TAG: Returned when a new tag is found (that is, <tag>)

TEXT: Returned when text is found (that is, <tag>text</tag> where text has

been found)

END_TAG: Returned when the end of tag is found (that is, </tag>)

END_DOCUMENT: Returned when the end of the XML file is reached

Additionally, the parser can be set to validate the input.Typically, parsing without validation

is used when under constrained memory environments, such as a mobile environment.

Compliant, nonvalidating parsing is the default for this XML Pull Parser.

 

 

0 件のコメント:

コメントを投稿