解析Android的RSS源

时间:2014-07-04 19:02:03

标签: android xml xml-parsing rss

这是一个示例RSS提要。

http://na.leagueoflegends.com/en/rss.xml

<channel>
  <title>Most Recent News</title>
  <link>http://na.leagueoflegends.com/en/rss.xml</link>
  <description/>
  <language>en-US</language>
  <item>
    <title>Community-recommended bundles available now </title>
    <link>http://na.leagueoflegends.com/en/news/store/sales/community-recommended-bundles-available-now?ref=rss</link>
    <description>&lt;div class="field field-name-field-body-small field-type-text-long field-label-hidden">Craving something new at champ select? Get fed on these limited-time bundles based on players’ suggestions!&lt;/div>&lt;div class="field field-name-field-article-media field-type-file field-label-hidden">&lt;div id="file-11675" class="file file-image file-image-jpeg">  &lt;img typeof="foaf:Image" src="/sites/default/files/styles/large/public/upload/bundle-banner_1.jpg?itok=4MD_leb5" width="480" height="270" alt="" title="" />&lt;/div>&lt;/div>&lt;div class="field field-name-custom-author field-type-entityreference field-label-hidden">&lt;span class="article_detail">&lt;span class="posted_by">By WizardCrab&lt;/span>&lt;/span>&lt;/div></description>
    <pubDate>Mon, 30 Jun 2014 19:35:11 +0000</pubDate>
    <dc:creator>sbroome</dc:creator>
    <guid isPermaLink="false">18268 at http://na.leagueoflegends.com</guid>
  </item>
  <item>
    <title>Champion and skin sale: 07.04 - 07.07</title>
    <link>http://na.leagueoflegends.com/en/news/store/sales/champion-and-skin-sale-0704-0707?ref=rss</link>
    <description>&lt;div class="field field-name-field-body-small field-type-text-long field-label-hidden">Grab these champions and skins on sale for 50% off for a limited time:&lt;/div>&lt;div class="field field-name-field-article-media field-type-file field-label-hidden">&lt;div id="file-13873" class="file file-image file-image-jpeg">  &lt;img typeof="foaf:Image" src="/sites/default/files/styles/large/public/upload/mu842qnf.jpg?itok=OktrEQdQ" width="480" height="270" alt="" title="" />&lt;/div>&lt;/div>&lt;div class="field field-name-custom-author field-type-entityreference field-label-hidden">&lt;span class="article_detail">&lt;span class="posted_by">By WizardCrab&lt;/span>&lt;/span>&lt;/div></description>
    <pubDate>Tue, 24 Jun 2014 21:24:10 +0000</pubDate>
    <dc:creator>sbroome</dc:creator>
    <guid isPermaLink="false">18060 at http://na.leagueoflegends.com</guid>
  </item>
  <item>
   ....
   ....
   .... (and goes on for a while)

我想在。内获得eat属性。

我目前正在使用

            URL rssFeedUrl = new URL(rssUrl);
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            XMLReader xmlReader = saxParser.getXMLReader();
            RSSHandler rssHandler = new RSSHandler();
            xmlReader.setContentHandler(rssHandler);
            InputSource inputSource = new InputSource(rssFeedUrl.openStream());
            xmlReader.parse(inputSource);


private class RSSHandler extends DefaultHandler {

public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
                if (localName.equals("item")) {
                    item = true;
                }
                if(item) {
                    if(localName.equals(RSS_TITLE)) {

                    } else if(localName.equals(RSS_LINK)) {

                    } else if(localName.equals(RSS_DESCRIPTION)) { 

                    } else if(localName.equals(RSS_PUB_DATE)) {

                    } else if(localName.equals(RSS_CREATOR)) {

                    } else if(localName.equals(RSS_GUID)) {

                    }

                }
                if (!localName.equals("item") && item == true) {
                    rssResult = rssResult + "\n\n" + localName + ": ";
                }


            }

            public void endElement(String namespaceURI, String localName, String qName) throws SAXException {

            }

            public void characters(char[] ch, int start, int length) throws SAXException {
                String cdata = new String(ch, start, length);
                if (item == true) {
                    String x = (cdata.trim()).replaceAll("\\s+", " ");
                    rssResult = rssResult + x + "\t";
                }
            }

有更简单的方法吗?因为现在它太繁琐了。我基本上想说getElement(&#34; description&#34;)

3 个答案:

答案 0 :(得分:3)

正如CommonsWare所提到的,如果你想保持简单,你应该使用第三方RSS库。 Android Arsenal中最新的RSS库是我制作的:PkRSS https://github.com/Pkmmte/PkRSS

API非常简单而且非常灵活。

// Loads your RSS feed in a background thread
PkRSS.with(this).load(url).async();

// After it has finished loading, use this to get the result
List<Article> articleList = PkRSS.with(this).get(url);

当然,如果您访问此库的GitHub页面,还有更多详细信息。

答案 1 :(得分:2)

我使用的是第三方RSS解析库。目前有四个列在the Android Arsenal上的RSS类别中。

如果没有,如果RSS文件可能很大,我会使用SAX而不是抱怨。如果您确定RSS文件很小,那么DOM解析器将为您提供更符合您所寻求的API的API,但它往往更慢且占用更多内存。

答案 2 :(得分:0)

我发现整个SAX很难看。我为RSS编写了自己的解析器,Atom&amp; RDF。

在GitHub上的AndroidWithoutStupid库中尝试MvNewsFeed类。 https://github.com/vsubhash/AndroidWithoutStupid

MvGeneral.startSyncDownload("http://www.example.com/rss.xml", "/mnt/sdcard/rss.xml");
MvNewsFeed oFeed = new MvNewsFeed("/mnt/sdcard/rss.xml");
for (int i = 0; i < oFeed.moMessages.size(); i++) {
  Log.d("YOUR_TAG", oFeed.moMessages.get(i).msMessageTitle);
  Log.d("YOUR_TAG", oFeed.moMessages.get(i).msMessageContent);
  Log.d("YOUR_TAG", oFeed.moMessages.get(i).msMessageLink);
}

为简洁起见,我使用了同步下载方法。异步下载有一个单独的类 - MvAsyncDownload。使用相同的参数调用构造函数。