Select to view content in your preferred language

Parsing *.gpx-files with JDOM/XPath

1985
1
05-17-2011 06:09 AM
DominikWiedner
Deactivated User
Hi,

Currently I'm working on an addin which should import data from a .gpx-file. To achieve this I want to use JDOM with XPath, but when I get to parse the data, nothing ever happens. My addin still runs and eclipse doesn't show any kind of errors.

The relevant line of code is:

Element testing = (Element) XPath.selectSingleNode(gpxDoc, "/gpx/trk/@*");


Is there anybody, who has faced the same kind of problem?

Regards,
Dominik
0 Kudos
1 Reply
DominikWiedner
Deactivated User
Just in case, someone is facing the same problem, the solution is to use JDOM only. Since the use of JDOM and XPATH results, for unknown reasons, in an untraceable error (ArcMap and the addin continue to run and you also can choose another file to parse, but you won't get any results!).

So you have to use JDOM only!

The resulting code is rather intricate:

private void LoadGPXTracks(File importFile) throws JDOMException, IOException, ParseException
 {
   //XML-Datei einladen
   Document gpxDoc = new SAXBuilder().build(importFile);
   Namespace gpx_ns = Namespace.getNamespace("http://www.topografix.com/GPX/1/1");
   Element gpx_root = gpxDoc.getRootElement();
  
   int j = 0;
   for (Iterator it = gpx_root.getChildren("trk", gpx_ns).iterator(); it.hasNext();)
   {
    Element trk = (Element) it.next();
    
    if("trk".equals(trk.getName()))
    {
     IFeature feature = _featureClassTracks.createFeature();
     int contractorFieldIndex = _featureClassTracks.findField("objname");
     if (contractorFieldIndex >= 0)
      feature.setValue(contractorFieldIndex, trk.getChildText("name", gpx_ns).toString());
     contractorFieldIndex = _featureClassTracks.findField("kern_id");
     if (contractorFieldIndex >= 0)
      feature.setValue(contractorFieldIndex, j++);
     
     IGeometryBridge2 pGeoBrg = new GeometryEnvironment();
     IPointCollection4 pPointColl = new Polyline();
     
     //List<?> trackSegsInfo = XPath.selectNodes(gpxDoc, "/gpx/trk/trkseg/");
     
     int i = 0;
     
     Element trk_seg = null;
     
     for (Iterator it_seg = trk.getChildren("trkseg", gpx_ns).iterator(); it_seg.hasNext();)
     {
      trk_seg = (Element) it_seg.next();
      for (Iterator trk_pt_count = trk_seg.getChildren("trkpt", gpx_ns).iterator(); trk_pt_count.hasNext();)
      {
       Element trk_pt_count_elem = (Element) trk_pt_count.next();
       i++;
      }
     }
     
     Point point_help = new Point();
     IPoint aPointBuffer = point_help;
     IPoint[] aPointBufferArray = new Point;
     i = 0;
         
     for (Iterator it_pt = trk_seg.getChildren("trkpt", gpx_ns).iterator(); it_pt.hasNext();)
     {
      Element trk_pt = (Element) it_pt.next();
      
      String longitude = null;
      String latitude =  null;
      
      Iterator it_trk_pt_attr = trk_pt.getAttributes().iterator(); it_trk_pt_attr.hasNext();
      Attribute trk_pt_attr = (Attribute) it_trk_pt_attr.next();
      latitude =  trk_pt_attr.getValue().toString();
      trk_pt_attr = (Attribute) it_trk_pt_attr.next();
      longitude = trk_pt_attr.getValue().toString();
      
      String elevation = trk_pt.getChildText("ele", gpx_ns).toString();
      String time = trk_pt.getChildText("time", gpx_ns).toString();
      
      Point point = CreatePoint(longitude, latitude, elevation != null ? elevation : "0");
      aPointBuffer = point;
      aPointBufferArray = aPointBuffer;
      
      IFeature trkPntFeature = _featureClassTrackPoints.createFeature();
      trkPntFeature.setShapeByRef(point);
      if (time != null)
      {
       DateFormat format = new SimpleDateFormat("yyyy'-'MM'-'dd'T'hh:mm:ss");
       Date d = format.parse(time);
       trkPntFeature.setValue(trkPntFeature.getFields().findField("time"), d);
      }
      trkPntFeature.store();
      
      i++;
     }
     
     pGeoBrg.setPoints(pPointColl, aPointBufferArray);
     
     feature.setShapeByRef((IPolyline) pPointColl);
     feature.store();
    }
    it.next();
   }  
 }


Regards,
Dominik
0 Kudos