Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8545185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:44:09+00:00 2026-06-11T12:44:09+00:00

I’m totally stumped on this DOM XML Parser at the moment. I’m getting the

  • 0

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:

<?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>

The XML Parser I put together is as follows:

        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);
        }
    }

`

I’m seeing output that is wrong…

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

Why is point_name ALWAYS “Right” and point_cmt ALWAYS “Turn right onto Grouse Dr”?
How do I fix this?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:44:11+00:00Added an answer on June 11, 2026 at 12:44 pm

    I decided to use JOOX to get it done because the code is easier and simpler to understand…

    public void parseXMLFile(String fullpath) {
        String metadata_name = null;
        String metadata_link_href = null;
        String metadata_link_name = null;
        String metadata_time = null;
        String point_lat = null;
        String point_lon = null;
        String last_lat = null;
        String last_lon = null;
        String point_name = null;
        String point_cmt = null;
        String fileContents = null;
    
        try {
            fileContents = readFileAsString(fullpath);
    
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        try {
            Document doc = (Document) loadXMLFromString(fileContents);
    
            for (Match metadata : $(doc).find("metadata").each()) {
                Match c = metadata.children();
                for (int i = 0; i < c.size(); i++) {
                    metadata_name = c.find("name").text();
                    metadata_link_href = c.find("link").attr("href").toString();
                    metadata_link_name = c.find("link").find("text").text();
                    metadata_time = c.find("time").text();
                }
            }
    
            for (Match rte2 : $(doc).xpath("//rte").andSelf().each()) {
                rte2.children();
                for(Match rtetag : rte2.children().each()) {
                    if(rtetag.tag().equalsIgnoreCase("name")) {
                        rtetag.text();
                    }
                    if(rtetag.tag().equalsIgnoreCase("rtept")) {
                        for(Match rtepttag : rtetag.children().andSelf().each()) {
                            if(rtepttag.tag().equals("name")) {
                                point_name = rtepttag.text();
                            }
                            if(rtepttag.tag().equals("cmt")) {
                                point_cmt = rtepttag.text();
                            }
                            if(rtepttag.tag().equals("rtept")) {
                                if(last_lon == null) {
                                    last_lon = point_lon;
                                }
                                if(last_lat == null) {
                                    last_lat = point_lat;
                                }
                                point_lat = rtepttag.attr("lat");
                                point_lon = rtepttag.attr("lon");
    
                                //done parsing? here's some log stuff to check
                                 Log.d("XML", "META_NAME:" + metadata_name);
                                 Log.d("XML", "META_LINK_HREF:" + metadata_link_href);
                                 Log.d("XML", "META_LINK_NAME:" + metadata_link_name);
                                 Log.d("XML", "META_TIME:" + metadata_time);
                                 Log.d("XML", "POINT_NAME:" + point_name);
                                 Log.d("XML","POINT_CMT:"+point_cmt);
                                 Log.d("XML", "POINT_LAT:" + point_lat);
                                 Log.d("XML", "POINT_LON:" + point_lon);
    
                            }
                        }
                    }
                }
            }
    
    
        } catch (Exception e) {
            String estr = e.getStackTrace().toString();
            Log.d("XML", "XML Exception: " + e + ":" + estr);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.