Monday, June 13, 2011

Android: Reading, using and working with XML data and web services in Android

One of the most powerful aspects of any mobile application for a 3G phone is that it can connect to the Internet. By connecting to the Internet the application can offer much more value to the user since it becomes an interface for a web-based component, e.g. using Twitter’s API to create a Twitter application so that you can get your Twitter updates without having to open the mobile browser. The most common way of interfacing with a web-based component is by using web services in XML format.

While trying to developer my own app which reads a web service from my own server, I ran into a lot of difficulties in implementing the client that consumes the web service. Android does not have libraries for XPath handling of XML documents, so it makes deciphering XML data a little bit more difficult. From what I’ve read online the Android team is currently working on including such libraries in future versions.

After some digging around I found an amazing link that shows different methods for consuming an XML file in Android and parsing through it without the use of XPaths. The link is this: Working with XML on Android. To start off, this link is an absolute must-read. Everything that I am going to write in my post here relates to this link. The code offered on that webpage uses polymorphism to show you 4 different methods of working with XML data. It provides a fully-functional Android application and all the source code for it. The source code can be found here: AndroidXML.zip.

My post today will concentrate on how to customize the code from the application in the above link, in order to read and parse your own XML data. If you are a Java pro, you might not need this post. My Java is a little rusty, so I needed some time to figure out exactly what I had to change and where in order to get this to work with my own web service XML. Now that I’ve figured it out, I thought I’d share it. In my next post I will give the simplified version of this code – where there is no polymorphism, and thus there are only the minimum number of classes needed to implement this XML-reading solution.


So until I post the simplified source code for working with XML data in Android, here are some tips on getting through the larger polymorphism-based source code and customizing it for your own XML data:

1. First off, read over the link Working with XML on Android as much as you can.


2. Download the source code for the Android application that they offer: AndroidXML.zip.

3. Import the project into your Eclipse workspace by right-clicking in Project Explorer and selecting “Import”.

01 Import

4. Select “Existing Projects into Workspace”

5. Browse to the directory where you extracted the ZIP file with the source code and then click on the Next buttons to finish off the wizard. The project is called “AndroidXml”.

6. You will now see the project in your workspace:

02 project

7. Here is a quick breakdown of what some of those Java classes do:

- MessageList.java is the main activity that gets started. It lists the items from the XML data using a ListActivity. In this project the items come from an RSS feed.


- FeedParser.java, FeedParserFactory.java, BaseFeedParser.java, RSSHandler.hava are all classes that this particular example uses to set the framework for polymorphism.

- This example uses 4 methods for grabbing the XML data and reading it.The 4 methods that this example uses are:

1. AndroidSaxFeedParser.java (the default)

2. DomFeedParser.java

3. SaxFeedParser.java

4. XmlPullFeedParser.java

These 4 classes all extend BaseFeedParser.java.

8. In order to customize this for your own XML file you need to edit the following places (assuming you are using AndroidSaxFeedParser, which is the default):

- FeedParserFactory.java: you need to change the URL location of the web service or XML document in the global variable here:


static String feedUrl = "http://www.androidster.com/android_news.rss";

- AndroidSaxFeedParser.java: you need to change the root node of your XML document. This is stored in the String called RSS.

static final String RSS = "RootNode";

- BaseFeedParser.java: you need to change this class according to the nodes that your XML document has.


The nodes CHANNEL and ITEM refer to the nodes <Channel> and <Item> in the RSS feed that this example uses. You need to change them to mimic your nodes from your XML document:

static final String CHANNEL = "channel";
static final  String ITEM = "item";

The other constants that are declared refer to the nodes for each repeating item.

static final  String PUB_DATE = "pubDate";
static final  String DESCRIPTION = "description";
static final  String LINK = "link";

static final  String TITLE = "title";

For this particular example, since an RSS feed XML document is used, it has repeating nodes for <Description>, <Link>, <Title>, <PubDate>. You need to change this structure to mimic your structure.


Note: Remember that if you change the name of the constants (as opposed to the value of the constants), you will need to change other classes which call these constants.

- If you change the names of the constants, you will have to update AndroidSaxFeedParser.java in this section:

item.getChild(TITLE).setEndTextElementListener(new EndTextElementListener(){
            public void end(String body) {
                currentMessage.setTitle(body);
            }
        });
        item.getChild(LINK).setEndTextElementListener(new EndTextElementListener(){
            public void end(String body) {
                currentMessage.setLink(body);
            }
        });
        item.getChild(DESCRIPTION).setEndTextElementListener(new EndTextElementListener(){
            public void end(String body) {
                currentMessage.setDescription(body);
            }
        });
        item.getChild(PUB_DATE).setEndTextElementListener(new EndTextElementListener(){
            public void end(String body) {
                currentMessage.setDate(body);
            }
        });

As you can see this section is hardcoded for the 4 nodes that are expected in this XML document (TITLE, LINK, DESCRIPTION, PUB_DATE). You will need to change this section and hardcode this for your own nodes.

- If you change the names of the higher-level nodes, i.e. <Channel> and <Item>, then you need to update the following section of AndroidSaxFeedParser.java:

Element itemlist = root.getChild(CHANNEL);
Element item = itemlist.getChild(ITEM);

And that is all. The customized code will use the AndroidSaxParser implementation of an XML Parser, it will go to the URL you provided in FeedParserFactory.java, and it will iterate through the updated nodes as you have labeled them in BaseFeedParser.java and AndroidSaxFeedParser.java.

In my next post I will provide a simplified version of this code, which does not use polymorphism. It will (hopefully) use the minimum required classes to get XML data and parse it.


No comments:

Post a Comment