android如何解析原子提要?

时间:2012-10-01 10:04:10

标签: android atom-feed

我正在执行基于从picasa公开相册中检索图像的任务。它以原子进给格式返回响应。以下是我从picasa获取的响应代码段。

<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时,这是logcat(“RSSHandler”,“localName =”+ localName +“,qName =”+ qName);

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:来自此回复的小组。我试过了this,但我无法得到结果。任何人都可以帮我这样做。谢谢。

1 个答案:

答案 0 :(得分:0)

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