Why is this XML DOM Parser Not Parsing the rtept, name, and cmt tags correctly?

3l33t33n

New Member
I'm totally stumped on this DOM XML Parser at the moment. I'm getting the wrong info for two of the tags and I can't figure out why those two tags (name and cmt) keep having the same info instead of the correct info.The XML I'm using is as follows:\[code\]<?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>\[/code\]The XML Parser I put together is as follows:\[code\] public void parseXMLFile(String fullpath) { String metadata_name = null; String metadata_link_href = http://stackoverflow.com/questions/12435587/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 = http://stackoverflow.com/questions/12435587/attributes.getNamedItem("href") .getNodeValue(); metadata_link_href = http://stackoverflow.com/questions/12435587/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); } }\[/code\]`I'm seeing output that is wrong...\[code\]09-15 03:37:20.963: D/XML(14951): NODELISTLENGTH3:509-15 03:37:20.963: I/XML(14951): name node:Home Test09-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/171447509-15 03:37:20.963: I/XML(14951): time node:2012-09-13T16:08:10Z09-15 03:37:20.963: I/XML(14951): rte_name:Home Test09-15 03:37:20.963: I/XML(14951): point_lat:39.4128809-15 03:37:20.963: I/XML(14951): point_lon:-77.4629700000000409-15 03:37:20.963: I/XML(14951): point_name:Right09-15 03:37:20.963: I/XML(14951): point_cmt:Turn right onto Grouse Dr09-15 03:37:20.973: D/XML(14951): NODELISTLENGTH3:509-15 03:37:20.973: I/XML(14951): name node:Home Test09-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/171447509-15 03:37:20.973: I/XML(14951): time node:2012-09-13T16:08:10Z09-15 03:37:20.973: I/XML(14951): rte_name:Home Test09-15 03:37:20.973: I/XML(14951): point_lat:39.4133309-15 03:37:20.973: I/XML(14951): point_lon:-77.462409-15 03:37:20.973: I/XML(14951): point_name:Right09-15 03:37:20.973: I/XML(14951): point_cmt:Turn right onto Grouse Dr\[/code\]Why is point_name ALWAYS "Right" and point_cmt ALWAYS "Turn right onto Grouse Dr"?How do I fix this?Thanks in advance.
 
Back
Top