2013-11-22 119 views
1

也許我正在討論這個錯誤的方式,我試圖解析一個gpx文件,我從來沒有在android之前解析過xml格式,我嘗試了幾種不同的方法,似乎無法得到任何結果。Xml解析Android與XPath

xml位於底部(它真的很長!)。我需要trkpt節點,實際上是我需要的唯一數據,經緯度和時間。

我累了的最後一種方法是使用Xpath。它總是返回一個空的節點列表。

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

也試過這樣:

private List<Location> 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 < nodelist_trk.getLength(); j++) { 
       Node node = nodelist_trk.item(j); 
       if (node.getNodeName().equals("trkseg")) { 
        NodeList trkpntList = node.getChildNodes(); 

XML:

<?xml version="1.0" ?> 
<gpx> 
<name><![CDATA[WassonPeak]]></name> 
<desc><![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.]]></desc> 
<author><![CDATA[Scott Morris]]></author> 
<email><![CDATA[[email protected]]]></email> 
<url><![CDATA[http://www.topofusion.com]]></url> 
<urlname><![CDATA[TopoFusion Home Page]]></urlname> 
<keywords><![CDATA[Wasson Saguaro West]]></keywords> 
<bounds maxlat="32.288840" minlon="-111.150076" minlat="32.265301" maxlon="-111.120627"/> 
<wpt lat="32.273882" lon="-111.147150"> 
<name><![CDATA[Wasson Peak]]></name> 
<cmt><![CDATA[]]></cmt> 
</wpt> 
<wpt lat="32.288552" lon="-111.120627"> 
<name><![CDATA[Camino Del Cerro Trailhead]]></name> 
<cmt><![CDATA[]]></cmt> 
</wpt> 
<wpt lat="32.265516" lon="-111.143047"> 
<name><![CDATA[Saddle]]></name> 
<cmt><![CDATA[]]></cmt> 
</wpt> 
<trk> 
<url><![CDATA[http://www.topofusion.com]]></url> 
<urlname><![CDATA[TopoFusion Home Page]]></urlname> 
    <trkseg> 
    <trkpt lat="32.288668" lon="-111.120915"> 
     <ele>847.058838</ele> 
     <time>2002-11-20T23:05:06Z</time> 
    </trkpt> 
    <trkpt lat="32.288668" lon="-111.120915"> 
     <ele>847.539551</ele> 
     <time>2002-11-20T23:05:07Z</time> 
    </trkpt> 
    <trkpt lat="32.288668" lon="-111.120915"> 
     <ele>847.058838</ele> 
     <time>2002-11-20T23:05:08Z</time> 
    </trkpt> 

回答

0

你的第一個方法是,你可以把工作做的方式之一!

String path = "StringPath.xml"; 
InputStream is = new FileInputStream(new File(ruta)); 
InputSource inputSrc = new InputSource(is); 

XPath xpath = XPathFactory.newInstance().newXPath(); 
String expression = "//trkpt"; 
NodeList nodes = (NodeList)xpath.evaluate(expression, inputSrc, XPathConstants.NODESET); 
    for (int i=0; i < nodes.getLength(); i++){ 
     NamedNodeMap node = nodes.item(i).getAttributes(); 
     System.out.println(node.toString()); 
    } 

當我試着使用上面的XML代碼,我得到的終端

3項