Android: 如何解析这个XML解析。我想解析一个RSS源。我的问题是如何解析<item>
和</item>
标签之间的所有标签。
<channel>
<title>Pub post</title>
<atom:link href="http://abishtpub.wordpress.com/feed/" rel="self"
type="application/rss+xml"/>
<link>http://abishtpub.wordpress.com</link>
<description>A fine WordPress.com site</description>
<item>
<title>multi-content</title>
<media:content url="http://1.gravatar.com/avatar/afb466de71a710f1e901250488e9ebd6? s=96&d=identicon&r=G" medium="image">
<media:title type="html">abisht</media:title>
</media:content>
<media:content url="http://abishtpub.files.wordpress.com/2014/06/modern-medicine.jpg?w=300" medium="image">
<media:title type="html">modern-medicine</media:title>
</media:content>
<media:content url="http://abishtpub.files.wordpress.com/2014/06/frisky_nursing_home.jpg?w=300" medium="image">
<media:title type="html">frisky_nursing_home</media:title>
</media:content>
</item>
这是我用来解析值的代码。
注意:但我只得到一个值。我的问题是如何在所有标签中获取所有值。
private String readItem(XmlPullParser parser) throws XmlPullParserException, IOException {
RSSItem rssItem = new RSSItem();
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
String result = "";// new SpannedString("");
parser.require(XmlPullParser.START_TAG, null, "item");
while (parser.next()!= XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
Log.d("debug","item:name:"+name);
if (name.equals("title")) {
rssItem.title = readTitle(parser);
}else if(name.equals("link")){
rssItem.link = readlink(parser);
} else if (name.equals("media:content")) {
result = readMedia(parser);
}
}
listData.add(rssItem);
return result;
}