android - 为 Android 解析 RSS 提要

标签 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("description")

最佳答案

正如 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 页面,还有更多详细信息。

关于android - 为 Android 解析 RSS 提要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24579701/

相关文章:

xml - 在 Clojure 中搜索 xml

java - 在 Java 中从 DOM 解析器创建对象?

java - 在 Javax.xml.ws 服务上添加 XMLParserV2 jar 时发生冲突

java - 如何从firebase数据库获取所有子节点?

Android:读取正在运行的应用程序的开始时间。只能用于服务吗?

java - qt项目代码到xml文件转换

java - 在 Java 中解析 HTML 页面

java - Android系统如何告诉应用程序该应用程序已进入后台(不可见)?

android - Android Eclipse 模拟器中的 map 、Facebook、电子邮件、Youtube 错误

python - OpenERP 7 用户角色的访问权限