我正在尝试从 tomcat 服务器接收一个 xml 文件并对其进行解析。
但 xmlPullParser 事件在 xml 末尾返回 TEXT(值:4)。
为什么返回 4?如何解决这个问题呢?
[这是xml代码。]
由 OSX 的 texteditor 编写。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<school_record>
<imagename>mark_goodschool.png</imagename>
<code>001</code>
<name>good_elementschool</name>
<address>inhawaii</address>
<tel>123-123-1234</tel>
<homepage>http://elementschool.org</homepage>
</school_record>
</resources>
[这是解析代码。]
Thread t_downloadRes = new Thread(new Runnable() {
@Override
public void run() {
try {
//Download xml File and parse it.
URL url = new URL(STR_SERVER_URL + STR_SERVER_XML);
InputStream is = url.openStream();
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(new InputStreamReader(is, "utf-8"));
int eventType = parser.getEventType();
AA_SchoolDTO stu = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_TAG:
String startTag = parser.getName();
if ("school_record".equals(startTag)) {
stu = new AA_SchoolDTO();
}
if (startTag.equals("imagename")) {
stu.setStr_imageName(parser.nextText());
}
if (startTag.equals("code")) {
stu.setStr_code(parser.nextText());
}
if (startTag.equals("name")) {
stu.setStr_name(parser.nextText());
}
if (startTag.equals("address")) {
stu.setStr_address(parser.nextText());
}
if (startTag.equals("tel")) {
stu.setStr_tel(parser.nextText());
}
if (startTag.equals("homepage")) {
stu.setStr_homPage(parser.nextText());
}
break;
case XmlPullParser.END_TAG:
String endTag = parser.getName();
if ("school_record".equals(endTag))
allSchoolList.add(stu);
break;
}
eventType = parser.next(); //// <== return event TEXT (value : 4) at the end of xml.
}
is.close();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}