Android: Simplified source code for parsing and working with XML data and web services in Android

Posted July 19th @ 10:05 pm by Boyan

In my previous post I linked to a terrific website (Working with XML on Android) which describes how you can read and parse XML documents in Android. The code supplied by that website used polymorphism to show 4 different methods for parsing the XML data. I vowed to simplify that and share the new source code.

To download the AndroidXmlSimple project click here. You will be taken to another page where you can click to download to the ZIP file.

Below are some instructions on setting yourself up with this source code and customizing it for your own XML data.

1. Download the file AndroidXmlSimple.zip

2. Unzip it on your disk.

3. Open Eclipse and import the Android project into your workspace.

01 Import

4. The project will look like this:

02 project

5. This new project opens up an XML document (the same RSS feed as the original example) and displays it using a ListActivity.

6. The classes are:

- MessageList.java: the main Activity

- Message.java: object that stores the parsed XML data

- RssHandler.java: object that parses the XML data

- BaseFeedParser.java: object that initiates the starting point and configurations for the XML data that should be parsed.

7. To customize this project for your own feed, edit the file BaseFeedParser.java.

Update the URL location here:

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

Update the hierarchy of nodes here:

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

Update the node names that repeat here:

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

And as in the previous post, if you change any names of the constants, you will need to update other sections of the code-base, for example below in BaseFeedParser.java, and in a few places in RssHandler.java.

        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);
            }
        });

 

Please leave a comment if you download this ZIP file – I’d like to know if you found this useful. And let me know if it does what you expected after reading my post. Thanks!

Advertisement


-->

20 Comments

  1. Ventrix
    August 11, 2009 at 08:35

    Very nice try and good example!

    Unfortunately it works only for RSS 2.

    RDF does now work, and it’s too common…

  2. android2
    October 18, 2009 at 15:07

    hello I read posts from kml and put them into a listview someone help me know

  3. Sumit Asok
    December 19, 2009 at 02:31

    You code is superb!.

    But a little bit hard for beginners.

    You can write a tutorial itself with those code.
    I am waiting for that. ( a little bit code explanation )

    Keep the good work on.

  4. Galip
    January 16, 2010 at 14:17

    Hey thanks for the code it helped me A LOT.

    I’m making an app for Android that uses a few webservices and your simple code works. It saved me a lot of time.

    Anyways, I have a question.
    One of my webservices don’t work. Or better said, I can’t seem to get it to work :)

    It’s this webservice:
    http://www.pathe.nl/export/export.asmx/HaalAgenda?filmId=0&biosId=6

    Can you (or someone else) tell me how I can parse this kind of webservice (cuz this is not a regular XML-service)

    Thanks in advance!

    Galip

  5. Stephen
    January 25, 2010 at 14:59

    Thanks for publishing that. I’d found the IBM page before and this did indeed make it that bit simpler. I haven’t been able to run the code yet as I’m at work and fighting with the Android emulator to get it through the proxy. This did however lead me to try making the XML file local (which is my final aim anyway) as res/xml/android_news.xml and tried loading it by using:

    Xml.parse(appContext.getResources().openRawResource(R.xml.android_news), Xml.Encoding.UTF_8, root.getContentHandler());

    The debugger tells me it’s not well formed. Googling around for solutions at the moment but it seems everyone loading local files uses getResources().getXml(blah) and uses a PullParser.

    If you’ve got any advice I’d love to hear it. Thanks again.

  6. Felicia
    February 2, 2010 at 23:27

    I have a question, i working on a currency exchange.
    So i need to the live data of different countries..
    So how shall go about to extract out the value of it?

    Email me: shiqin02@hotmail.com

  7. RandiR
    February 23, 2010 at 13:41

    I am in the same boat as Felicia. She needs to extract live currency info, I need to extract live stock quotes. I do it with biterscripting ( http://www.biterscripting.com ). Google up “biterscripting stock price” and it will find lots of scripts people have written. Hope this helps.

  8. asas
    February 27, 2010 at 08:04

    I cannot download the zip-file, can you fix the page?

    Warning: Cannot modify header information - headers already sent by (output started at /home/warrlou2/public_html/blog/downloads.php:8) in /home/warrlou2/public_html/blog/downloads.php on line 76″

  9. Boyan
    February 27, 2010 at 18:23

    Sorry about that, I will try to fix it.

  10. Boyan
    February 27, 2010 at 19:12

    Ok, the button should be working now. Thanks for notifying me of this!

  11. monu
    March 17, 2010 at 06:33

    code is very good …
    and it help me a lot..
    but in this code if want to change web address then will we have to change hierarchy of nodes.. and how can i find out the tag names in site which is subscribe to getting news. like here pubdate, description, title etc..
    thanks and if u have solution plz mail me

  12. Kevin
    March 25, 2010 at 05:07

    HI,
    Code is very useful but i have a doubt. If the XML file contains the attributes like

    How do we write the BasefeedParser in the List parse parse() method .
    Here , it can retrieve the content anything between the tags like content

    Please help asap.. Thanks in advance..

  13. Ahmed
    April 11, 2010 at 20:01

    This is very helpful as I’m trying to make an kuler (adobe) for android, but how can I pass a picture to the RSS feed and the message list?
    http://kuler-api.adobe.com/rss/get.cfm?listtype=recent&key=E476E7121D5776A8831DAA6476DE8837

  14. frankie
    April 19, 2010 at 07:35

    Does this also parse the “cdata” ? As a matter of fact i tried it but it is not supporting cdata tags . Can you suggest any modifications so as to parse the “cdata” tag also

    thanks

  15. Steven
    April 28, 2010 at 15:07

    Great Tutorial! Got me up and running in less then 15 minutes. Exactly what I’ve been looking for!

  16. diamant-x
    April 28, 2010 at 19:01

    Hi!
    Thanks for this code, I’ll try to adapt it to a slightly different XML structure.
    Allthought that, something weird is going on when I try to run this code without modyfing it.
    In a Virtual Emulator on th ePc it’s ok, but when I run it on an Android Device directly the List can’t be load.
    I activated the debug mode and this is what comes up:

    04-29 00:58:12.707: ERROR/AndroidNews(10820): Caused by: java.lang.RuntimeException: java.text.ParseException: Unparseable date: Tue, 24 Nov 2009 01:50:21 +0100
    04-29 00:58:12.707: ERROR/AndroidNews(10820): at com.warriorpoint.androidxmlsimple.Message.setDate(Message.java:57)
    04-29 00:58:12.707: ERROR/AndroidNews(10820): at com.warriorpoint.androidxmlsimple.BaseFeedParser$5.end(BaseFeedParser.java:77)
    04-29 00:58:12.707: ERROR/AndroidNews(10820): at android.sax.RootElement$Handler.endElement(RootElement.java:197)
    04-29 00:58:12.707: ERROR/AndroidNews(10820): at org.apache.harmony.xml.ExpatParser.endElement(ExpatParser.java:159)
    04-29 00:58:12.707: ERROR/AndroidNews(10820): at org.apache.harmony.xml.ExpatParser.append(Native Method)
    04-29 00:58:12.707: ERROR/AndroidNews(10820): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:506)
    04-29 00:58:12.707: ERROR/AndroidNews(10820): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:467)
    04-29 00:58:12.707: ERROR/AndroidNews(10820): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:329)
    04-29 00:58:12.707: ERROR/AndroidNews(10820): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:286)
    04-29 00:58:12.707: ERROR/AndroidNews(10820): at android.util.Xml.parse(Xml.java:87)
    04-29 00:58:12.707: ERROR/AndroidNews(10820): at com.warriorpoint.androidxmlsimple.BaseFeedParser.parse(BaseFeedParser.java:81)
    04-29 00:58:12.707: ERROR/AndroidNews(10820): … 15 more

    Don’t know whats the matter about it ^^’. If You could tell please. thanks!

  17. Juan
    May 25, 2010 at 08:40

    Thanks a lot! It really helped me.
    One hint maybe:
    Xml.parse(this.getInputStream(), Xml.Encoding.ISO_8859_1, root.getContentHandler());
    if your are trying to parse ISO :)

  18. kevin
    May 27, 2010 at 04:41

    HI,
    CAn anyone help me how to extract the attribute values in the XMl file. I’m able to retrieve the normal data inside the tags but i need for the like example below:

    i want to extract the value kevin and age value 20. how to modify the item code for the same????

  19. Andrew
    July 15, 2010 at 19:52

    Can Someone please show us how to extract attribute values?!?!?! I have been trying to learn java for android and this is the farthest i have gotten. Please someone post how to!

  20. calmchess
    July 15, 2010 at 22:06

    Well i downloaded your code however eclipse complains
    Project has no default.properties file so i can’t use your simplified version luckily enough i’m a good enough programmer to yank out everything except the SAX code and make it work…..you should simplify all 4 parse methods as they all have a use in one use case or another.

Leave a comment

Standard Login

Options:

Colors