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

原文地址:http://www.warriorpoint.com/blog/2009/07/19/android-simplified-source-code-for-parsing-and-working-with-xml-data-and-web-services-in-android/

 

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!

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值