Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

xml - Parse RSS feed for Android

This is an example RSS feed.

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)

I wanted to get eat attribute within the .

I am currently using

            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 + "

" + 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 + "";
                }
            }

Is there a simpler way? Cause right now it is too tedious. I basically want to say getElement("description")

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just as CommonsWare mentioned, you should probably use a third party RSS library for this if you want to keep it simple. The newest RSS library in the Android Arsenal is one I made: PkRSS https://github.com/Pkmmte/PkRSS

The API is extremely simple yet flexible.

// 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);

Of course, there are a lot more details if you visit the GitHub page for this library.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...