android - 在 Android 中解析 RSS 提要

标签 android parsing rss

Android:如何解析THIS XML PARSE 我想解析一个 RSS 提要。我的问题是如何解析 <item> 之间的所有标签和 </item>标签。

  <channel>
 <title>Pub post</title>
 <atom:link href="http://abishtpub.wordpress.com/feed/" rel="self"              
 type="application/rss+xml"/>
 <link>http://abishtpub.wordpress.com</link>
  <description>A fine WordPress.com site</description>

  <item>
    <title>multi-content</title>

      <media:content url="http://1.gravatar.com/avatar/afb466de71a710f1e901250488e9ebd6? s=96&d=identicon&r=G" medium="image">
      <media:title type="html">abisht</media:title>
          </media:content>
      <media:content url="http://abishtpub.files.wordpress.com/2014/06/modern-medicine.jpg?w=300" medium="image">
      <media:title type="html">modern-medicine</media:title>
          </media:content>
      <media:content url="http://abishtpub.files.wordpress.com/2014/06/frisky_nursing_home.jpg?w=300" medium="image">
      <media:title type="html">frisky_nursing_home</media:title>
          </media:content>
   </item>

这是我用来解析值的代码。

NOTE:但我只得到一个<media:content> value我的问题是如何获得全部<media:content>所有值 <items>标签。

   private String readItem(XmlPullParser parser)    throws XmlPullParserException, IOException {
            RSSItem rssItem = new RSSItem();
            ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
            String result = "";// new SpannedString("");
            parser.require(XmlPullParser.START_TAG, null, "item");              

            while (parser.next()!= XmlPullParser.END_TAG) {
                if (parser.getEventType() != XmlPullParser.START_TAG) {
                    continue;
                }
                String name = parser.getName();
                Log.d("debug","item:name:"+name);
                if (name.equals("title")) {
                    rssItem.title = readTitle(parser);
                }else if(name.equals("link")){                      
                    rssItem.link = readlink(parser);
                } else if (name.equals("media:content")) {
                    result = readMedia(parser);
                }  

            }
            listData.add(rssItem);
            return result;
        }

最佳答案

我刚刚尝试了在我的一篇帖子下发布的 url。

这是要解析的代码

 XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(false);
            XmlPullParser xpp = factory.newPullParser();
            InputStream is = new ByteArrayInputStream(res.getBytes());
            xpp.setInput(is,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("Title is",xpp.nextText()); 
                    } else if (xpp.getName().equalsIgnoreCase("link")) {
                        if (insideItem)
                            Log.i("Link is",xpp.nextText()); 
                    }
                    else if (xpp.getName().equalsIgnoreCase("comments")) {
                        if (insideItem)
                            Log.i("Comment is.",xpp.nextText());  
                    }
                    else if (xpp.getName().equalsIgnoreCase("pubDate")) {
                        if (insideItem)
                            Log.i("Publish Date is.",xpp.nextText());  
                    }
                    else if (xpp.getName().equalsIgnoreCase("media:content")) {
                        if (insideItem)
                            Log.i("Media Content url is.",xpp.getAttributeValue(null, "url"));  
                    }
                    else if (xpp.getName().equalsIgnoreCase("media:title")) {
                        if (insideItem)
                            Log.i("Media Content title.",xpp.nextText());  
                    }

                } else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) {
                    insideItem = false;
                }

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

日志

07-14 12:16:02.683: I/Title is(2204): multi-content
07-14 12:16:02.683: I/Link is(2204): http://abishtpub.wordpress.com/2014/06/20/multi-content/
07-14 12:16:02.683: I/Comment is.(2204): http://abishtpub.wordpress.com/2014/06/20/multi-content/#comments
07-14 12:16:02.693: I/Publish Date is.(2204): Fri, 20 Jun 2014 13:04:07 +0000
07-14 12:16:02.693: I/Media Content url is.(2204): http://1.gravatar.com/avatar/afb466de71a710f1e901250488e9ebd6?s=96&d=identicon&r=G
07-14 12:16:02.693: I/Media Content title.(2204): abisht
07-14 12:16:02.693: I/Media Content url is.(2204): http://abishtpub.files.wordpress.com/2014/06/modern-medicine.jpg?w=300
07-14 12:16:02.693: I/Media Content title.(2204): modern-medicine
07-14 12:16:02.693: I/Media Content url is.(2204): http://abishtpub.files.wordpress.com/2014/06/frisky_nursing_home.jpg?w=300
07-14 12:16:02.693: I/Media Content title.(2204): frisky_nursing_home
07-14 12:16:02.693: I/Title is(2204): my next post
07-14 12:16:02.693: I/Link is(2204): http://abishtpub.wordpress.com/2014/06/19/my-next-post/
07-14 12:16:02.693: I/Comment is.(2204): http://abishtpub.wordpress.com/2014/06/19/my-next-post/#comments
07-14 12:16:02.693: I/Publish Date is.(2204): Thu, 19 Jun 2014 13:24:26 +0000
07-14 12:16:02.693: I/Media Content url is.(2204): http://1.gravatar.com/avatar/afb466de71a710f1e901250488e9ebd6?s=96&d=identicon&r=G
07-14 12:16:02.693: I/Media Content title.(2204): abisht
07-14 12:16:02.693: I/Title is(2204): Title test
07-14 12:16:02.693: I/Link is(2204): http://abishtpub.wordpress.com/2014/06/19/title-test/
07-14 12:16:02.693: I/Comment is.(2204): http://abishtpub.wordpress.com/2014/06/19/title-test/#comments
07-14 12:16:02.693: I/Publish Date is.(2204): Thu, 19 Jun 2014 13:11:17 +0000
07-14 12:16:02.693: I/Media Content url is.(2204): http://1.gravatar.com/avatar/afb466de71a710f1e901250488e9ebd6?s=96&d=identicon&r=G
07-14 12:16:02.693: I/Media Content title.(2204): abisht

注意:我没有检查是否所有的标签都被解析了。请自行检查。

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

相关文章:

java - 微调器禁用滑动打开

ios - JSON解析向iOS返回null(json字符串看起来正确)

android - 借助原子解析来解析

mysql - 使用 XSLT 将 RSS pubDate 转换为 mySQL 时间戳格式

android - 为什么 JmDNS 服务发现在运行 Android 2.1-update1 的摩托罗拉 Droid 上工作,而不在运行相同 Android 版本的 HTC Incredible 上工作?

java - 如何计算价格通过从firebase数据库检索一个值并通过elegantNumberButton检索其他值

android - 帮助纠正 Android OpenGL 2.0 w/QCAR 中的线缩放

parsing - 如何让 Bison 摆脱所有错误

c++ - 如何将字符串解析为已知结构 C++ 中的数字

xml - RSS 阅读器的工作原理(netvibes、Google 阅读器...)