我看过这个。所以请不要把我重定向到那个网站。
我已经使用过 DOM 解析器,这是我的问题。
我有相同的 XML:
<MyResource>
<Item>First</Item>
<Item>Second</Item>
</MyResource>
现在我没有办法,只能通过 PullParser 解析这个 XML。我的方法取自此:
private List readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
// TODO Auto-generated method stub
List entries = new ArrayList();
parser.require(XmlPullParser.START_TAG, ns, "MyResource");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
// Starts by looking for the entry tag
if (name.equals("MyResource")) {
entries.add(readEntry(parser));
} else {
skip(parser);
}
}
return entries;
}
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
// TODO Auto-generated method stub
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;
}
}
}
private Item readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
// TODO Auto-generated method stub
parser.require(XmlPullParser.START_TAG, ns, "Item");
String text = null;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("Item")) {
text = readMyText(parser);
} else {
skip(parser);
}
}
return new Item(text);
}
private String readMyText(XmlPullParser parser) throws XmlPullParserException, IOException {
// TODO Auto-generated method stub
parser.require(XmlPullParser.START_TAG, ns, "Item");
String text = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "Item");
return text;
}
private String readText(XmlPullParser parser) throws XmlPullParserException, IOException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
我得到这个作为解析的文本:
null
null
谁能帮我解决这个问题??