在android中解析特定的rss feed

时间:2012-12-27 23:24:44

标签: android parsing rss tags

我试图在我的Android应用中解析rss feed。 我的Feed包含很多带标签的商品" tag" 它看起来像

<item>
<title> title </title>
<link> link </link>
<pubDate> date </pubDate>
<description> description </description>

<tags>
<tag id="1">first</tag>
<tag id="2">second</tag>
<tag id="3">third</tag>
</tags>

</item>

我的问题: 如何选择仅使用特定&#34;标记&#34;例如。标签=&#34;第二&#39;?

1 个答案:

答案 0 :(得分:0)

重写了我的Xml Factory课程,它应该引导你走上正确的轨道。

/**
 * 
 * @author hsigmond
 *
 */
public class RssXmlFactory {

    public static ArrayList<RSSItem> parseResult(final String rssDataContent,String tag_id) throws ParserConfigurationException,
            SAXException, IOException {

        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        public String mItemTagID=tag_id;//"2"

        RSSItemsHandler parser = new RSSItemsHandler();
         xr.setContentHandler(parser);
        StringReader sr = new StringReader(rssDataContent);
        InputSource is = new InputSource(sr);
        xr.parse(is);

        return parser.mItemList;

    }

}


class RSSItemsHandler extends DefaultHandler {

    private StringBuilder mSb = new StringBuilder();
    public ArrayList<RSSItem> mItemList = new ArrayList<RSSItem>();
    public RSSItem mCurrentRssItem = null;
    public String mItemTitle="";


    @Override
    public void startElement(final String namespaceURI, final String localName, final String qName,
            final Attributes atts) throws SAXException {
        mSb.setLength(0);

        if (localName.equals(XMLTag.TAG_RSS_ITEM_ROOT)) {
            /** Get the rss item title attribute value */
            mItemTitle=atts.getValue(XMLTag.TAG_RSS_ITEM_TITLE);
            //#TODO Log result
        }

        else if (localName.equals(XMLTag.TAG_RSS_ITEM_TAG_ROOT)) {
        //This is where you check if the TAG equals id=2, did not have the time to check if it works yet, it's late...
        if(atts.getValue(XMLTag.TAG_RSS_ITEM_TAG_ID).equalsIgnoreCase(mItemTagID)){//id="2"
             mCurrentRssItem = new RSSItem();
            /** Set item title attribute value */
            mCurrentRssItem.title=mItemTitle;
                //#TODO Log result
          }     
        }
    }

    @Override
    public void endElement(final String namespaceURI, final String localName, final String qName) throws SAXException {

        if (localName.equals(XMLTag.TAG_RSS_ITEM_ROOT)) {
            mItemList.add(mCurrentRssItem);

        } else if (localName.equals(XMLTag.TAG_RSS_ITEM_TAG_ROOT)) {
            mCurrentRssItem.tag = mSb.toString();         
        } 
    }

    @Override
    public void characters(final char[] ch, final int start, final int length) throws SAXException {
        super.characters(ch, start, length);
        mSb.append(ch, start, length);
    }
}
}

有关如何在Android上处理XML的更多详细信息,请查看此处:http://www.ibm.com/developerworks/opensource/library/x-android/index.html