android下对xml的解析
android 解析xml
1.使用SAXParser
// TODO: switch to sax XmlPullParser xpp = Xml.newPullParser(); xpp.setInput(in, null); // null = default to UTF-8 int eventType; String title = ""; String link = ""; String description = ""; eventType = xpp.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_TAG) { String tag = xpp.getName(); if (tag.equals("item")) { title = link = description = ""; } else if (tag.equals("title")) { xpp.next(); // Skip to next element -- assume text is directly inside the tag title = xpp.getText(); } else if (tag.equals("link")) { xpp.next(); link = xpp.getText(); } else if (tag.equals("description")) { xpp.next(); description = xpp.getText(); } } else if (eventType == XmlPullParser.END_TAG) { // We have a comlete item -- post it back to the UI // using the mHandler (necessary because we are not // running on the UI thread). String tag = xpp.getName(); if (tag.equals("item")) { RssItem item = new RssItem(title, link, description); mHandler.post(new ItemAdder(item)); } } eventType = xpp.next(); }