改寫了一個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
取決於解析器界面。顯示一些代碼 –