现在的位置: 首页 > 综合 > 正文

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

2018年04月13日 ⁄ 综合 ⁄ 共 2432字 ⁄ 字号 评论关闭

原文地址: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!

 

 

抱歉!评论已关闭.