如何使用XmlPullParser解析RSS提要?

时间:2013-07-02 19:25:34

标签: android rss xmlpullparser

我想解析一个RSS提要。我的问题是如何解析<item></item>标记之间的所有标记。

鉴于这个非常简单的XML:

<?xml version="1.0" ?>
<rss version="2.0">
<channel>
  <title>MyRSSPage</title>
  <link>http://www.example.com</link>
  <item>
  <link>www.example.com/example1</link>
  <title>Example title 1</title>
  </item>
  <item>
  <link>www.example.com/example2</link>
  <title>Example title 2</title>
  </item>
</channel>
</rss>

我想解析<item>...</item>标记之间的内容。

            List<RssMessage> messages = new ArrayList<RssMessage>();

            // parser is a XmlPullParser instance
            while(parser.next() != XmlPullParser.END_DOCUMENT) {
                if (parser.getEventType() != XmlPullParser.START_TAG) {
                    continue;
                }
            String name = parser.getName();
            // START OF HEADER
            if(name.equals("title")) {
                title = parser.nextText();
            }
            else if(name.equals("link")) {
                link = parser.nextText();
            }
            else if(name.equals("description")) {
                description = parser.nextText();
            }
            else if(name.equals("language")) {
                language = parser.nextText();
            }
            else if(name.equals("copyright")) {
                copyright = parser.nextText();
            }
            else if(name.equals("pubDate")) {
                pubdate = parser.nextText();
            }
            // END OF HEADER

            else if(name.equals("item")) {
                RssMessage rssMessage = processItem(parser);
                messages.add(rssMessage);
            }
        }

在下面的方法中,我想解析<item>...</item>标签内的标签。如何构建一个仅在<item></item>之间浏览项目的循环?

编辑 这几乎正​​常。但有时即使RSS xml DO中存在相应的元素,也不会启动所有元素!以下代码有问题吗?

private RssMessage processItem(XmlPullParser parser) throws IOException, XmlPullParserException {
        RssMessage rssMessage = new RssMessage();
    parser.require(XmlPullParser.START_TAG, ns, "item");
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        if(name.equals("link")) {
            rssMessage.setLink(parser.nextText());
        }
        else if(name.equals("guid")) {
            rssMessage.setGuid(parser.nextText());
        }
        else if(name.equals("category")) {
            rssMessage.setCategory(parser.nextText());
        }
        else if(name.equals("title")) {
            rssMessage.setTitle(parser.nextText());
        }
        else if(name.equals("pubDate")) {
            rssMessage.setPubDate(parser.nextText());
        }
    }
    return rssMessage;
    }

1 个答案:

答案 0 :(得分:12)

尝试以下方法。

try {
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(false);
    XmlPullParser xpp = factory.newPullParser();
    xpp.setInput(url.openConnection().getInputStream(), "UTF_8"); 
    //xpp.setInput(getInputStream(url), "UTF-8");

    boolean insideItem = false;

    // Returns the type of current event: START_TAG, END_TAG, etc..
    int eventType = xpp.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_TAG) {

            if (xpp.getName().equalsIgnoreCase("item")) {
                insideItem = true;
            } 
            else if(xpp.getName().equalsIgnoreCase("title")) 
            {

            }
        }
        eventType = xpp.next(); //move to next element
    }

} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (XmlPullParserException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

编辑:

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(open,null);
// xpp.setInput(getInputStream(url), "UTF-8");

boolean insideItem = false;

// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
    if (eventType == XmlPullParser.START_TAG) {

        if (xpp.getName().equalsIgnoreCase("item")) {
            insideItem = true;
        } else if (xpp.getName().equalsIgnoreCase("title")) {
            if (insideItem)
                Log.i("....",xpp.nextText()); // extract the headline
        } else if (xpp.getName().equalsIgnoreCase("link")) {
            if (insideItem)
                Log.i("....",xpp.nextText());  // extract the link of article
        }
    } else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) {
        insideItem = false;
    }

    eventType = xpp.next(); // move to next element
}

输出

www.example.com/example1
Example title 1
www.example.com/example2
Example title 2