我很困惑如何解析这个xml并转到下一个标签
rss -> channel -> item
的xml树像这样的结构,请帮助我通过它。提前谢谢。
XML文件链接
<rss>
<channel>
<item>
<link> </link>
<title> </title>
</item>
</channel>
android Java code:
public class RssXmlParser {
private static final String ns = null;
public List<Entry> parse(InputStream in) throws XmlPullParserException, IOException {
try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in, null);
parser.nextTag();
return readFeed(parser);
} finally {
in.close();
}
}
private List<Entry> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
List<Entry> entries = new ArrayList<Entry>();
parser.require(XmlPullParser.START_TAG, ns, "channel");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("item")) {
entries.add(readEntry(parser));
} else {
skip(parser);
}
}
return entries;
}
public static class Entry {
public final String url;
public final String title;
private Entry(String url,String title) {
this.url = url;
this.title=title;
}
}
private Entry readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "item");
String title = null;
String link = null;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("title")) {
title = readTitle(parser);
} else if (name.equals("link")) {
link = readLink(parser);
} else {
skip(parser);
}
}
return new Entry(link,title);
}
private String readTitle(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "title");
String title = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "title");
return title;
}
private String readLink(XmlPullParser parser) throws IOException, XmlPullParserException {
String link = "";
parser.require(XmlPullParser.START_TAG, ns, "link");
link = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "link");
return link;
}
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
if (parser.getEventType() != XmlPullParser.START_TAG) {
throw new IllegalStateException();
}
int depth = 1;
while (depth != 0) {
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth--;
break;
case XmlPullParser.START_TAG:
depth++;
break;
}
}
}
}
链接在这里
在此解析中重复解析直到事件为结束文档,当事件类型为开始标记时,然后检查期望标记从中获取值示例代码如下:获取what_tagyou_want标签中Name属性的值
int event = myparser.getEventType();
while (event != XmlPullParser.END_DOCUMENT)
{
String name=myparser.getName();
switch (event)
{
case XmlPullParser.START_TAG:
if(name.equals("What_tagyou_want"))
{
namexml= myparser.getAttributeValue(null,"Name")+" ";//get values from here
}
break;
case XmlPullParser.END_TAG:
break;
default: break;
}
event = myparser.next();
}
下面是XML pull解析的完整示例:
https://androidgarden.wordpress.com/2016/09/02/android-xml-pull-parsing-from-sd-card-assets/