我看了this。所以请不要将我重定向到那个网站。
我已经使用过DOM解析器,here是我的问题。
我使用相同的XML:
<MyResource>
<Item>First</Item>
<Item>Second</Item>
</MyResource>
现在我无法通过PullParser解析这个XML。 我的方法取自this:
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
任何人都可以帮我解决这个问题吗?
答案 0 :(得分:0)
请参阅this链接,它有一个很好的例子
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader ("<foo>Hello World!</foo>"));
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_DOCUMENT) {
System.out.println("Start document");
} else if(eventType == XmlPullParser.END_DOCUMENT) {
System.out.println("End document");
} else if(eventType == XmlPullParser.START_TAG) {
System.out.println("Start tag "+xpp.getName());
} else if(eventType == XmlPullParser.END_TAG) {
System.out.println("End tag "+xpp.getName());
} else if(eventType == XmlPullParser.TEXT) {
System.out.println("Text "+xpp.getText());
}
eventType = xpp.next();
}
答案 1 :(得分:0)
我可以提供一个示例代码来解析您的XML。你去吧,
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
boolean flag=false;
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader ("<MyResource><Item>First</Item> <Item>Second</Item>/MyResource>"));
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_TAG) {
System.out.println("Start tag "+xpp.getName());
if(xpp.getName().toString().equals("MyResource"){
flag=true;
}
else if(xpp.getName().toString().equals("Item"){
if(flag){
String test=xpp.nextText().toString();
//do whatever u want with the data
}
}
} else if(eventType == XmlPullParser.END_TAG) {
if(xpp.getText().equals("MyResource")
flag=false;
}
eventType = xpp.next();
}