在android中解析特定的rss feed

时间:2021-12-02 01:09:12

i'm trying to parse rss feed in my android app. and my feed contains a lot of items with tags "tag" it looks like

我正在尝试在我的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>

my question: how can i select items only with specific "tag" eg. tag="second'?

我的问题:我怎样才能选择只有特定“标签”的物品,例如。标签=“第二”?

1 个解决方案

#1


0  

rewrote a Xml Factory class I had, it should lead you on the right track.

重写了我的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);
    }
}
}

For more details on how to handle XML on Android look here: http://www.ibm.com/developerworks/opensource/library/x-android/index.html

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

#1


0  

rewrote a Xml Factory class I had, it should lead you on the right track.

重写了我的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);
    }
}
}

For more details on how to handle XML on Android look here: http://www.ibm.com/developerworks/opensource/library/x-android/index.html

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