【问题标题】:android How to parse atom feed?android如何解析原子提要?
【发布时间】:2012-09-22 04:19:24
【问题描述】:

我正在执行一项基于从 picasa 公共相册中检索图像的任务。它以原子提要格式返回响应。这是从 picasa 得到的响应 sn-p im。

<item>
<guid isPermaLink="false">https://picasaweb.google.com/data/entry/base/user/112104498664640193446/albumid/5791036084570489841/photoid/5791036081578935442?alt=rss&amp;hl=en_US</guid>
<pubDate>Sat, 22 Sep 2012 16:17:38 +0000</pubDate>
<atom:updated>2012-09-24T19:11:05.625Z</atom:updated>
<category domain="http://schemas.google.com/g/2005#kind">http://schemas.google.com/photos/2007#photo</category>
<title>SACHIN-TENDULKAR-WALLPAPER-51[1].jpg</title>
<description>&lt;table>&lt;tr>&lt;td style="padding: 0 5px">&lt;a href="https://picasaweb.google.com/112104498664640193446/Sachin?authkey=Gv1sRgCMG2v_GY6-iiOg#5791036081578935442">&lt;img style="border:1px solid #5C7FB9" src="https://lh3.googleusercontent.com/-IQEQp8boLEg/UF3kokv7hJI/AAAAAAAAACk/cr53Fr8fUC4/s288/SACHIN-TENDULKAR-WALLPAPER-51%25255B1%25255D.jpg" alt="SACHIN-TENDULKAR-WALLPAPER-51[1].jpg"/>&lt;/a>&lt;/td>&lt;td valign="top">&lt;font color="#6B6B6B">Date: &lt;/font>&lt;font color="#333333">Sep 22, 2012 4:17 PM&lt;/font>&lt;br/>&lt;font color=\"#6B6B6B\">Number of Comments on Photo:&lt;/font>&lt;font color=\"#333333\">0&lt;/font>&lt;br/>&lt;p>&lt;a href="https://picasaweb.google.com/112104498664640193446/Sachin?authkey=Gv1sRgCMG2v_GY6-iiOg#5791036081578935442">&lt;font color="#3964C2">View Photo&lt;/font>&lt;/a>&lt;/p>&lt;/td>&lt;/tr>&lt;/table></description>
<enclosure type="image/jpeg" url="https://lh3.googleusercontent.com/-IQEQp8boLEg/UF3kokv7hJI/AAAAAAAAACk/cr53Fr8fUC4/SACHIN-TENDULKAR-WALLPAPER-51%25255B1%25255D.jpg" length="0"/>
<link>https://picasaweb.google.com/112104498664640193446/Sachin?authkey=Gv1sRgCMG2v_GY6-iiOg#5791036081578935442</link>
<media:group>
<media:content url="https://lh3.googleusercontent.com/-IQEQp8boLEg/UF3kokv7hJI/AAAAAAAAACk/cr53Fr8fUC4/SACHIN-TENDULKAR-WALLPAPER-51%25255B1%25255D.jpg" height="384" width="512" type="image/jpeg" medium="image"/>
<media:credit>NW_MAY08</media:credit>
<media:description type="plain"/>
<media:keywords/>
<media:thumbnail url="https://lh3.googleusercontent.com/-IQEQp8boLEg/UF3kokv7hJI/AAAAAAAAACk/cr53Fr8fUC4/s72/SACHIN-TENDULKAR-WALLPAPER-51%25255B1%25255D.jpg" height="54" width="72"/>
<media:thumbnail url="https://lh3.googleusercontent.com/-IQEQp8boLEg/UF3kokv7hJI/AAAAAAAAACk/cr53Fr8fUC4/s144/SACHIN-TENDULKAR-WALLPAPER-51%25255B1%25255D.jpg" height="108" width="144"/>
<media:thumbnail url="https://lh3.googleusercontent.com/-IQEQp8boLEg/UF3kokv7hJI/AAAAAAAAACk/cr53Fr8fUC4/s288/SACHIN-TENDULKAR-WALLPAPER-51%25255B1%25255D.jpg" height="216" width="288"/>
<media:title type="plain">SACHIN-TENDULKAR-WALLPAPER-51[1].jpg</media:title>
</media:group>
</item>

编辑:

public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        //Only consider elements from allowed third-party namespaces
        if (NAMESPACES.contains(uri)) {
            mSb = new StringBuffer();
            String value = localName.trim();
            //Log.i(TAG, "start");
            if (value.equalsIgnoreCase("rss") ||  value.equalsIgnoreCase("rdf")) {
                isType = true;
            } else if (value.equalsIgnoreCase("feed")) {
                isType = true;
                isFeed = true;
            } else if (value.equalsIgnoreCase("channel")) {
                isFeed = true;
            } else if (value.equalsIgnoreCase("item") || value.equalsIgnoreCase("entry")) {
                mItem = new Item();
                imageLoader = new ImageLoader(context);
                isItem = true;      
            } else if (value.equalsIgnoreCase("title"))
                isTitle = true;
            else if (value.equalsIgnoreCase("link")) {
                // Get attributes from link element for Atom format
                if (attributes != null) {
                    // Enclosure for Atom format
                    if (attributes.getValue("rel") != null && attributes.getValue("rel").equalsIgnoreCase("enclosure")) {
                        mEnclosure = new Enclosure();
                        mMimeAttribute = attributes.getValue("type");
                        isEnclosure = true;
                    }
                    mHrefAttribute = attributes.getValue("href");
                }
                isLink = true;
            } else if (value.equalsIgnoreCase("pubDate") || value.equalsIgnoreCase("published") || value.equalsIgnoreCase("date"))
                isPubdate = true;
            else if (value.equalsIgnoreCase("guid") || value.equalsIgnoreCase("id"))
                isGuid = true;
            else if (value.equalsIgnoreCase("description") || value.equalsIgnoreCase("summary"))
                isDescription = true;
            else if (value.equalsIgnoreCase("encoded") || value.equalsIgnoreCase("content"))
                isContent = true;
            else if (value.equalsIgnoreCase("source"))
                isSource = true;
            else if(value.equalsIgnoreCase("media:group")){
                isMedia = true;
                Log.i("media", "media group");
            }
            else if (value.equalsIgnoreCase("enclosure")) {
                // Enclosure for RSS format
                if (attributes != null) {
                    mEnclosure = new Enclosure();
                    mMimeAttribute = attributes.getValue("type");
                    mHrefAttribute = attributes.getValue("url");
                    if(mMimeAttribute.equalsIgnoreCase("image/jpeg")){
                        mItem.setImageUrl(mHrefAttribute);
                    }
                    Bitmap bmp = imageLoader.getBitmap(mHrefAttribute);
                    mItem.setBitmapImage(bmp);
                    isEnclosure = true;
                }
            }else if(value.equalsIgnoreCase("media:content")){
                if(attributes != null){
                    mMediaContent = new MediaContent();
                    mMimeAttribute_media = attributes.getValue("type");
                    mHrefAttribute_media = attributes.getValue("url");
                }
            }
        }
    }

编辑 2

这是我使用 Log.i("RSSHandler", "localName="+localName+", qName="+qName); 时的 logcat;

10-02 14:04:51.799: I/RSSHandler(3090): localName=item, qName=item
10-02 14:04:51.799: I/RSSHandler(3090): localName=guid, qName=guid
10-02 14:04:51.799: I/RSSHandler(3090): localName=pubDate, qName=pubDate
10-02 14:04:51.799: I/RSSHandler(3090): localName=updated, qName=atom:updated
10-02 14:04:51.799: I/RSSHandler(3090): localName=category, qName=category
10-02 14:04:51.799: I/RSSHandler(3090): localName=title, qName=title
10-02 14:04:51.799: I/RSSHandler(3090): localName=description, qName=description
10-02 14:04:51.819: I/RSSHandler(3090): localName=enclosure, qName=enclosure
10-02 14:04:51.839: I/RSSHandler(3090): localName=link, qName=link
10-02 14:04:51.839: I/RSSHandler(3090): localName=group, qName=media:group
10-02 14:04:51.839: I/RSSHandler(3090): localName=content, qName=media:content
10-02 14:04:51.839: I/RSSHandler(3090): localName=credit, qName=media:credit
10-02 14:04:51.839: I/RSSHandler(3090): localName=description, qName=media:description
10-02 14:04:51.839: I/RSSHandler(3090): localName=keywords, qName=media:keywords
10-02 14:04:51.839: I/RSSHandler(3090): localName=thumbnail, qName=media:thumbnail
10-02 14:04:51.839: I/RSSHandler(3090): localName=thumbnail, qName=media:thumbnail
10-02 14:04:51.839: I/RSSHandler(3090): localName=thumbnail, qName=media:thumbnail
10-02 14:04:51.839: I/RSSHandler(3090): localName=title, qName=media:title

我尝试像这样修改代码:

else if(qName.trim().equalsIgnoreCase("media:content") || value.equalsIgnoreCase("content")){

                if (attributes != null) {
                    mMediaContent  = new MediaContent();
                    mHrefAttribute_media = attributes.getValue("url");
                    Log.i("media url", mHrefAttribute_media);
                }
                isMedia = true;
                Log.i("media group", "media content");
            }

但这不起作用。

我无法从该回复中读取 medai:group。我试过this 但我无法得到结果。谁能帮我做这件事。谢谢。

【问题讨论】:

  • 你是怎么解析的?你用的是哪个解析器?在标签media:group中,media是命名空间,group是元素名称。
  • @Rajesh 我正在使用 SAX 解析器。请参阅此链接。我使用相同的过程。 javaworld.com/javaworld/jw-02-2012/…
  • 请将代码 sn-p 贴在您阅读所需元素的位置。

标签: android atom-feed


【解决方案1】:

media:group 包含两部分 media,即命名空间前缀和group,即元素名称。

startElement的localName和qName参数在XML SAX: Explain result in `qName` and `localName` in one example XML file中有解释

所以你可以修改你的代码如下:

        else if(qName.equalsIgnoreCase("media:group")){
            isMedia = true;
            Log.i("media", "media group");
        }

【讨论】:

  • 请检查 LogCat 是否有异常。尝试添加调试日志语句并查看作为参数传递给 startElement 的内容。
  • 请查看Reading and Writing Logs,了解如何编写和使用日志语句。很抱歉,我无法在 gtalk 上聊天。
  • 没关系.. 但是如果我将 Log.i("", "") 放在 sn-p 的其他部分中,我可以看到日志
  • 尝试在 startElement 方法的开头添加Log.i("RSSHandler", "localName="+localName+", qName="+qName);
  • NAMESPACES 包含什么?您也可以尝试在日志语句中添加uri
猜你喜欢
  • 1970-01-01
  • 2014-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-28
  • 1970-01-01
相关资源
最近更新 更多