2012-12-27 72 views
0

我試圖解析rss訂閱源在我的android應用程序。 和我的飼料中含有大量的與標籤「標籤」項目 的,它看起來像解析android中的特定rss訂閱源

<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> 

我的問題: 我怎麼可以只選擇與特定的「標籤」,如項目。 ?標籤=「第二個」

+0

取決於解析器界面。顯示一些代碼 –

回答

0

改寫了一個xml工廠類我有,它應該引領你在正確的軌道上

/** 
* 
* @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