0

I have the following XML file containing GPS co-ordinates, and I was just wondering how I can extract the track latitudes and longitudes using Xpath in Java. So far I tried:

System.out.println(xpath.evaluate("/gpx/trk/trkseg/trkpt/@lat", doc));

but this only retrieves the first value? Please can someone tell me how to retrieve all the values in one go.

Many Thanks in advance.

<gpx xmlns="http://www.topografix.com/GPX/1/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" creator="MapSource 6.11.3"
version="1.1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1
http://www.topografix.com/GPX/1/1/gpx.xsd">
<metadata>...</metadata>
<wpt lat="40.653792" lon="-111.922379">...</wpt>
<wpt lat="40.658111" lon="-111.919564">...</wpt>
<wpt lat="40.659546" lon="-111.917527">...</wpt>
<wpt lat="40.595857" lon="-111.910294">...</wpt>
<wpt lat="40.657349" lon="-111.918721">...</wpt>
<trk>
<name>FromParking</name>
<extensions>...</extensions>
<trkseg>
<trkpt lat="40.653782" lon="-111.922365">
    <ele>1224.376221</ele>
    <time>2009-11-19T20:00:11Z</time>
</trkpt>
<trkpt lat="40.653786" lon="-111.922350">
    <ele>1223.895508</ele>
    <time>2009-11-19T20:00:13Z</time>
</trkpt>
<trkpt lat="40.654449" lon="-111.922073">
    <ele>1224.376221</ele>
    <time>2009-11-19T20:00:22Z</time>
</trkpt>
<trkpt lat="40.654509" lon="-111.921919">
    <ele>1224.376221</ele>
    <time>2009-11-19T20:00:25Z</time>
</trkpt>
<trkpt lat="40.654618" lon="-111.921700">
    <ele>1224.856934</ele>
    <time>2009-11-19T20:00:35Z</time>
</trkpt>
1
  • What XML/XPath library are you using? Commented Mar 5, 2014 at 12:30

2 Answers 2

1

Aside from the namespaces issue I described in my answer to your previous question, your fundamental problem here is that the XPath.evaluate methods without a QName returnType parameter will evaluate the XPath expression as a string, and the XPath rules say that the string value of a node set containing more than one node is defined to be the string value of the first node in the set in document order.

If you want to evaluate XPath expressions that return a set of nodes then you must use one of the XPath methods that takes a QName, pass XPathConstants.NODESET as the requested return type, and cast the result returned from evaluate to a org.w3c.dom.NodeList which you can then iterate over.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this: to retrieve all trkpt attributes do this:

String trkpt ="/gpx/trk/trkseg/trkpt";
NodeList nodeList= (NodeList) xpath.compile(trkpt).evaluate(doc, XPathConstants.NODESET);

for(int i=0; i<nodeList.getLength(); i++){
   Node node = nodeList.item(i);
   NamedNodeMap attributesMap = node.getAttributes();

   for (int j = 0; j< attributesMap.getLength(); j++) {            
     Node attributeNode = attributesMap.item(j);       
     String attributeName = attributeNode.getNodeName();        
     String attributeValue = attributeNode.getNodeValue();

     System.out.println(attributeName + "=" + attributeValue);
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.