title: Xml parsing in Android with XPath
body: <p>Perhaps I'm going about this the wrong way, I'm trying to parse a gpx file, I've never parsed xml format in android before, I've tried a few different ways and can't seem to get any results. </p>
  
  <p>The xml is at the bottom (a snippet its really long!).  I want the trkpt nodes, actually the only data I need it lat, lon and time.  </p>
  
  <p>The last method I tired was using Xpath.  It always returns an empty node list. </p>
  
  <pre><code>try {
          InputSource inputSrc = new InputSource(context.getResources().openRawResource(R.raw.sample_track));
  
          XPath xpath = XPathFactory.newInstance().newXPath();
          String expression = "//trkpt";
          NodeList nodes = (NodeList)xpath.evaluate(expression, inputSrc, XPathConstants.NODESET);
  </code></pre>
  
  <p>also tried this:</p>
  
  <pre><code>private List&lt;Location&gt; decodeGPX(Context context) {
          DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
          try {
              DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
              Document document = documentBuilder.parse(context.getResources().openRawResource(R.raw.sample_track));
              Element elementRoot = document.getDocumentElement();
  
              NodeList nodelist_trk = elementRoot.getElementsByTagName("trk");
              for (int j = 0; j &lt; nodelist_trk.getLength(); j++) {
                  Node node = nodelist_trk.item(j);
                  if (node.getNodeName().equals("trkseg")) {
                      NodeList trkpntList = node.getChildNodes();
  </code></pre>
  
  <p>XML:</p>
  
  <pre><code>&lt;?xml version="1.0" ?&gt;
  &lt;gpx&gt;
   &lt;name&gt;&lt;![CDATA[WassonPeak]]&gt;&lt;/name&gt;
   &lt;desc&gt;&lt;![CDATA[Wasson Peak is a classic hike in Saguaro National Park  West. The destination is the high point of the Tucson Mountains at 4687 feet, Wasson peak. This map is of the eastern approach, from the trailhead at the end of Camino del Cerro.]]&gt;&lt;/desc&gt;
   &lt;author&gt;&lt;![CDATA[Scott Morris]]&gt;&lt;/author&gt;
   &lt;email&gt;&lt;![CDATA[smorris@topofusion.com]]&gt;&lt;/email&gt;
   &lt;url&gt;&lt;![CDATA[http://www.topofusion.com]]&gt;&lt;/url&gt;
   &lt;urlname&gt;&lt;![CDATA[TopoFusion Home Page]]&gt;&lt;/urlname&gt;
   &lt;keywords&gt;&lt;![CDATA[Wasson Saguaro West]]&gt;&lt;/keywords&gt;
  &lt;bounds maxlat="32.288840" minlon="-111.150076" minlat="32.265301" maxlon="-111.120627"/&gt;
  &lt;wpt lat="32.273882" lon="-111.147150"&gt;
   &lt;name&gt;&lt;![CDATA[Wasson Peak]]&gt;&lt;/name&gt;
   &lt;cmt&gt;&lt;![CDATA[]]&gt;&lt;/cmt&gt;
  &lt;/wpt&gt;
  &lt;wpt lat="32.288552" lon="-111.120627"&gt;
   &lt;name&gt;&lt;![CDATA[Camino Del Cerro Trailhead]]&gt;&lt;/name&gt;
   &lt;cmt&gt;&lt;![CDATA[]]&gt;&lt;/cmt&gt;
  &lt;/wpt&gt;
  &lt;wpt lat="32.265516" lon="-111.143047"&gt;
   &lt;name&gt;&lt;![CDATA[Saddle]]&gt;&lt;/name&gt;
   &lt;cmt&gt;&lt;![CDATA[]]&gt;&lt;/cmt&gt;
  &lt;/wpt&gt;
  &lt;trk&gt;
   &lt;url&gt;&lt;![CDATA[http://www.topofusion.com]]&gt;&lt;/url&gt;
   &lt;urlname&gt;&lt;![CDATA[TopoFusion Home Page]]&gt;&lt;/urlname&gt;
    &lt;trkseg&gt;
      &lt;trkpt lat="32.288668" lon="-111.120915"&gt;
        &lt;ele&gt;847.058838&lt;/ele&gt;
        &lt;time&gt;2002-11-20T23:05:06Z&lt;/time&gt;
      &lt;/trkpt&gt;
      &lt;trkpt lat="32.288668" lon="-111.120915"&gt;
        &lt;ele&gt;847.539551&lt;/ele&gt;
        &lt;time&gt;2002-11-20T23:05:07Z&lt;/time&gt;
      &lt;/trkpt&gt;
      &lt;trkpt lat="32.288668" lon="-111.120915"&gt;
        &lt;ele&gt;847.058838&lt;/ele&gt;
        &lt;time&gt;2002-11-20T23:05:08Z&lt;/time&gt;
      &lt;/trkpt&gt;
  </code></pre>