我需要一些帮助来理解如何阅读以下XML:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<GetRouteSummaryForStopResponse xmlns="http://octranspo.com">
<GetRouteSummaryForStopResult>
<StopNo xmlns="http://tempuri.org/">5212</StopNo>
<StopDescription xmlns="http://tempuri.org/">BANNER / PARKMOUNT</StopDescription>
<Error xmlns="http://tempuri.org/" />
<Routes xmlns="http://tempuri.org/">
<Route>
<RouteNo>82</RouteNo>
<DirectionID>1</DirectionID>
<Direction>Westbound</Direction>
<RouteHeading>Bayshore</RouteHeading>
</Route>
<Route>
<RouteNo>282</RouteNo>
<DirectionID>0</DirectionID>
<Direction>Inbound</Direction>
<RouteHeading>Mackenzie King</RouteHeading>
</Route>
</Routes>
</GetRouteSummaryForStopResult>
</GetRouteSummaryForStopResponse>
</soap:Body>
</soap:Envelope>
我m trying to retrieve the text of <StopNo> and <StopDescription> and others after. To debug I
试图打印getName()和getText()。
//XmlPullParser - START
try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in, null);
while (parser.next() != XmlPullParser.END_DOCUMENT) {
Log.i("**********", parser.getName() + " " + parser.getText());
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
//Get Tag name
String name = parser.getName();
// Starts by looking for the temperature
if (name.equalsIgnoreCase("StopNo")) {
stopNumber = parser.getText();
Log.i("***** OS doInBackground", "stopNumber " + stopNumber);
}
//look for speed tag
if (name.equals("StopDescription")) {
stopDescription = parser.getText();
Log.i("***** OS doInBackground", "stopDescription " + stopDescription);
}
}
} catch (Exception e){
Log.i("****** Exception XML", e.getMessage());
}
if (in!=null) {
try {
in.close();
} catch (IOException e) {
}
}
但是,我得到以下输出。首先,它打印getName()开始标记,getText()= null,然后为标记和getText()=文本等打印null。
...
I/**********: StopNo null
I/***** OS doInBackground: stopNumber null
I/**********: null 5212
I/**********: StopNo null
I/**********: StopDescription null
I/***** OS doInBackground: stopDescription null
I/**********: null BANNER / PARKMOUNT
I/**********: StopDescription null
I/**********: Error null
I/**********: Error null
I/**********: Routes null
I/**********: Route null
I/**********: RouteNo null
I/**********: null 82
I/**********: RouteNo null
...
我无法理解为什么getName()和getText()都关闭了?
答案 0 :(得分:2)
遇到XmlPullParser.TEXT
时,文字即可使用。您正在阅读XmlPullParser.START_TAG
你可以试试这段代码
while (parser.next() != XmlPullParser.END_DOCUMENT) {
Log.i("**********", parser.getName() + " " + parser.getText());
switch (parser.getEventType()){
case XmlPullParser.START_TAG:
String name = parser.getName();
Log.i("***** OS doInBackground", "name " + name);
break;
case XmlPullParser.TEXT :
String text = parser.getText();
Log.i("***** OS doInBackground", "text " + text);
}
}