我在解析我的 xml 文档时遇到问题,我需要从以下部分获取值:坐标。解析和分段没有问题。
下面是我的 xml 文档和部分代码。有什么建议吗?
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>
<Placemark id="placemark1">
<name>test_name</name>
<description>geom_test</description>
<point>
<coordinates>40.751816, -73.959119</coordinates>
</point>
</Placemark>
<Placemark id="placemark2">
<name>test_name1</name>
<description>geom_test</description>
<point>
<coordinates>40.751816, -73.959119</coordinates>
</point>
</Placemark>
</Document>
</kml>
代码:
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("Placemark")) {
String id = null, date = null, pob = null;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
name = parser.getName();
if (name.equals("name")) {
id = readText(parser);
} else if (name.equals("description")) {
date = readText(parser);
} else if (name.equals("point")) {
parser.nextTag();
} else if (name.equals("coordinates")) {
pob = readText(parser);
}
}
myDbHelper.insertData(id, date, pob);