我现在完全被这个 DOM XML Parser 难住了。我得到了两个标签的错误信息,我无法弄清楚为什么这两个标签(名称和 cmt)保持相同的信息而不是正确的信息。
我使用的 XML 如下:
<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" version="1.1" creator="http://ridewithgps.com/">
<metadata>
<name>Home Test</name>
<link href="http://ridewithgps.com/routes/1714475">
<text>Home Test</text>
</link>
<time>2012-09-13T16:08:10Z</time>
</metadata>
<rte>
<name>Home Test</name>
<rtept lat="39.41333" lon="-77.4624">
<name>Right</name>
<cmt>Turn right onto Grouse Dr</cmt>
</rtept>
<rtept lat="39.41288" lon="-77.46297000000004">
<name>Right</name>
<cmt>Turn right onto Hunting Horn Ln</cmt>
</rtept>
<rtept lat="39.41431" lon="-77.46625">
<name>Right</name>
<cmt>Turn right onto Partridge Way</cmt>
</rtept>
<rtept lat="39.41431" lon="-77.46625">
<name>Left</name>
<cmt>Turn left onto Hunting Horn Ln</cmt>
</rtept>
<rtept lat="39.41288" lon="-77.46297000000004">
<name>Left</name>
<cmt>Turn left onto Grouse Dr</cmt>
</rtept>
<rtept lat="39.41333" lon="-77.4624">
<name>Right</name>
<cmt>Turn right onto Grouse Ct</cmt>
</rtept>
</rte>
</gpx>
我整理的 XML Parser 如下:
public void parseXMLFile(String fullpath) {
String metadata_name = null;
String metadata_link_href = null;
String metadata_link_name = null;
String metadata_time = null;
String rte_name = null;
Double point_lat = null;
Double point_lon = null;
Double last_lat = null;
Double last_lon = null;
String point_name = null;
String point_cmt = null;
Float distance_to = null;
// File f = new File(fullpath);
String fileContents = null;
try {
fileContents = readFileAsString(fullpath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Document doc = (Document) loadXMLFromString(fileContents);
doc.getDocumentElement().normalize();
Element root = doc.getDocumentElement();
Log.d("XML", "ROOTNODE:" + root.getNodeName());
NodeList nodeList = doc.getElementsByTagName("metadata");
Node nodee = nodeList.item(0);
Log.d("XML", "NODELISTLENGTH1:" +
nodee.getChildNodes().getLength());
for (int i = 0; i < nodee.getChildNodes().getLength(); i++) {
Node node = nodee.getChildNodes().item(i);
if (node.getNodeName().equalsIgnoreCase("name")) {
metadata_name = node.getTextContent();
} else if (node.getNodeName().equalsIgnoreCase("link")) {
NamedNodeMap attributes = node.getAttributes();
if (attributes.getNamedItem("href") != null) {
String href = attributes.getNamedItem("href")
.getNodeValue();
metadata_link_href = href;
}
metadata_link_name = node.getTextContent();
// Log.i("XML", "link node:" + node.getTextContent());
} else if (node.getNodeName().equalsIgnoreCase("time")) {
metadata_time = node.getTextContent();
}
}
// DO THE RTE NODES AFTER THIS
NodeList nodeList2 = doc.getElementsByTagName("rte");
Node nodee2 = nodeList2.item(0);
Log.d("XML", "NODELISTLENGTH2:"
+ nodee2.getChildNodes().getLength());
for (int i = 0; i < nodee2.getChildNodes().getLength(); i++) {
Node node = nodee2.getChildNodes().item(i);
if (node.getNodeName().equalsIgnoreCase("name")) {
rte_name = node.getTextContent();
} else if (node.getNodeName().equalsIgnoreCase("rtept")) {
NamedNodeMap attributes = node.getAttributes();
if (attributes.getNamedItem("lat") != null) {
// STORE THE LAST LAT FOR MEASURING DISTANCE BETWEEN
if (point_lat != null) {
last_lat = point_lat;
}
point_lat = Double.parseDouble(attributes.getNamedItem(
"lat").getNodeValue());
}
if (attributes.getNamedItem("lon") != null) {
// STORE THE LAST LON FOR MEASURING DISTANCE BETWEEN
if (point_lon != null) {
last_lon = point_lon;
}
point_lon = Double.parseDouble(attributes.getNamedItem(
"lon").getNodeValue());
}
NodeList rteptList = doc.getElementsByTagName("rtept");
// NodeList rteptList = node.getChildNodes();
Node nodee3 = rteptList.item(0);
Log.d("XML", "NODELISTLENGTH3:"
+ nodee3.getChildNodes().getLength());
for (int i1 = 0; i1 < nodee3.getChildNodes().getLength(); i1++) {
Node rteptNode = nodee3.getChildNodes().item(i1);
if (rteptNode.getNodeName().equalsIgnoreCase("name")) {
point_name = rteptNode.getFirstChild().getNodeValue();
} else if (rteptNode.getNodeName().equalsIgnoreCase("cmt")) {
point_cmt = rteptNode.getFirstChild().getNodeValue();
}
}
// DO THE DATABASE STORAGE HERE
Log.i("XML", "name node:" + metadata_name);
Log.i("XML", "link node:" + metadata_link_name);
Log.i("XML", "link attribute [href]:" + metadata_link_href);
Log.i("XML", "time node:" + metadata_time);
Log.i("XML", "rte_name:" + rte_name);
Log.i("XML", "point_lat:" + point_lat.toString());
Log.i("XML", "point_lon:" + point_lon.toString());
Log.i("XML", "point_name:" + point_name);
Log.i("XML", "point_cmt:" + point_cmt);
}
}
} catch (Exception e) {
String estr = e.getStackTrace().toString();
Log.d("XML", "XML Exception: " + e + ":" + estr);
}
}
`
我看到输出是错误的......
09-15 03:37:20.963: D/XML(14951): NODELISTLENGTH3:5
09-15 03:37:20.963: I/XML(14951): name node:Home Test
09-15 03:37:20.963: I/XML(14951): link node: Home Test
09-15 03:37:20.963: I/XML(14951): link attribute [href]:http://ridewithgps.com/routes/1714475
09-15 03:37:20.963: I/XML(14951): time node:2012-09-13T16:08:10Z
09-15 03:37:20.963: I/XML(14951): rte_name:Home Test
09-15 03:37:20.963: I/XML(14951): point_lat:39.41288
09-15 03:37:20.963: I/XML(14951): point_lon:-77.46297000000004
09-15 03:37:20.963: I/XML(14951): point_name:Right
09-15 03:37:20.963: I/XML(14951): point_cmt:Turn right onto Grouse Dr
09-15 03:37:20.973: D/XML(14951): NODELISTLENGTH3:5
09-15 03:37:20.973: I/XML(14951): name node:Home Test
09-15 03:37:20.973: I/XML(14951): link node: Home Test
09-15 03:37:20.973: I/XML(14951): link attribute [href]:http://ridewithgps.com/routes/1714475
09-15 03:37:20.973: I/XML(14951): time node:2012-09-13T16:08:10Z
09-15 03:37:20.973: I/XML(14951): rte_name:Home Test
09-15 03:37:20.973: I/XML(14951): point_lat:39.41333
09-15 03:37:20.973: I/XML(14951): point_lon:-77.4624
09-15 03:37:20.973: I/XML(14951): point_name:Right
09-15 03:37:20.973: I/XML(14951): point_cmt:Turn right onto Grouse Dr
为什么 point_name 总是“向右”而 point_cmt 总是“向右转到 Grouse Dr”?我该如何解决?
提前致谢。