2016-10-06 16 views
-2

SAXでgpxファイルの日付を解析するのに問題があります。どういうわけか、一部のノードの<time>タグを正しく取得できません。ほとんどのノードは大丈夫ですが、他のタグに問題はないようです。
異なるgpxファイルでテストしていますが、同じファイルの同じノードでは常に失敗し、特殊文字などはありません。それは周期的です。SAX Javaで日付を解析する

enter image description here

私は、エラーの次のノード番号を印刷しています。第3回列が

enter image description here

現在のエラーの間と最後のエラーの違いであり、通常は800〜1000の周りに最初のエラーを開始します。ご覧のとおり、日付が正しく選択されていないため、何らかの理由でトリミングされています。

これはjava.time使用しているクラスとあなたの日時形式パターンとの間に不一致があるように見えます私のハンドラ

package ec.com.mapachedev.webmapper.gpx; 

import java.time.LocalDateTime; 
import java.time.format.DateTimeFormatter; 
import java.util.ArrayList; 
import java.util.List; 

import org.xml.sax.Attributes; 
import org.xml.sax.SAXException; 
import org.xml.sax.helpers.DefaultHandler; 

public class SAXFileHandler extends DefaultHandler { 
    private String reading; 
    private List<SAXTrack> tracks; 
    private List<SAXWayPoint> waypoints; 
    private List<SAXTrackPoint> trackpoints; 
    private SAXTrack track; 
    private SAXTrackPoint trackPoint; 
    private SAXWayPoint wayPoint; 
    private LocalDateTime fechaExtraido; 
    private LocalDateTime fechaIni; 
    private LocalDateTime fechaFin; 
    private DateTimeFormatter parser = DateTimeFormatter 
      .ofPattern("[yyyy-MM-dd'T'HH:mm:ss.SSS'Z']" + "[yyyy-MM-dd'T'HH:mm:ss'Z']"); 
    private boolean isWaypoint = false; 
    private boolean isTrackPoint = false; 
    private int cont = 0; 

    public SAXFileHandler() { 
     super(); 
    } 

    @Override 
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 
     try { 
      if (qName.equals("trkpt")) { 
       trackPoint = new SAXTrackPoint(); 
       isWaypoint = false; 
       isTrackPoint = true; 
       try { 
        trackPoint.setLat(Double.parseDouble(attributes.getValue("lat"))); 
        trackPoint.setLon(Double.parseDouble(attributes.getValue("lon"))); 
       } catch (Exception e) { 
        System.out.println("Error " + e.getMessage()); 
       } 

      } /* 
       * else if (qName.equals("trkseg")) { 
       * 
       * } 
       */else if (qName.equals("trk")) { 
       track = new SAXTrack(); 
       isWaypoint = false; 
       isTrackPoint = false; 
      } else if (qName.equals("wpt")) { 
       isWaypoint = true; 
       isTrackPoint = false; 
       wayPoint = new SAXWayPoint(); 
       try { 
        wayPoint.setLat(Double.parseDouble(attributes.getValue("lat"))); 
        wayPoint.setLon(Double.parseDouble(attributes.getValue("lon"))); 
       } catch (Exception e) { 
        System.out.println("Error " + e.getMessage()); 
       } 
      } else if (qName.equals("gpx")) { 
       tracks = new ArrayList<SAXTrack>(); 
       waypoints = new ArrayList<SAXWayPoint>(); 

       isWaypoint = false; 
       isTrackPoint = false; 
      } 
     } finally { 
      // no matter the node, we empty the StringBuilder accumulator when 
      // we start 
      // a new node. 
      reading=null; 
     } 
    } 

    @Override 
    public void endElement(String uri, String localName, String qName) throws SAXException { 

     if (qName.equals("trkpt")) { 
      track.getPuntos().add(trackPoint); 
     } else if (qName.equals("speed")) { 
      try { 
       trackPoint.setVelocidad(Double.parseDouble(reading)); 
      } catch (Exception e) { 
       System.out.println("Error " + e.getMessage()); 
      } 
     } else if (qName.equals("time") && isTrackPoint) { 
      System.out.println("Time Track " + reading + " " 
        + cont++);/* 
           * try { cont++; 
           * //System.out.println("Time Track " + reading 
           * + " "+cont++); 
           * trackPoint.setFecha(LocalDateTime.parse(
           * reading, parser)); // Siempre guardamos la 
           * ultima fecha y si es la primer vez que // 
           * grabamos fechas tambien ponemos fechaInicial 
           * fechaFin = trackPoint.getFecha(); if 
           * (fechaIni == null) fechaIni = 
           * trackPoint.getFecha(); } catch (Exception e) 
           * { System.out.println("Error " + 
           * e.getMessage()+" " +cont); } 
           */ 
     } else if (qName.equals("ele") && isTrackPoint) { 
      try { 
       trackPoint.setEle(Double.parseDouble(reading)); 
      } catch (Exception e) { 
       System.out.println("Error " + e.getMessage()); 
      } 
     } else if (qName.equals("name") && isTrackPoint) { 
      // System.out.println("Nombre: "+reading); 
      trackPoint.setNombre(reading); 
     } else if (qName.equals("desc") && !isWaypoint && !isTrackPoint) { 
      track.setDesc(reading); 
     } else if (qName.equals("name") && !isWaypoint && !isTrackPoint) { 
      track.setName(reading); 
     } else if (qName.equals("desc") && isWaypoint) { 
      wayPoint.setDesc(reading); 
     } else if (qName.equals("cmt") && isWaypoint) { 
      wayPoint.setCmt(reading); 
     } else if (qName.equals("name") && isWaypoint) { 
      wayPoint.setCmt(reading); 
     } else if (qName.equals("time") && isWaypoint) { 
      System.out.println("Time WP " + reading); 
      wayPoint.setFecha(LocalDateTime.parse(reading, parser)); 
     } else if (qName.equals("ele") && isWaypoint) { 
      try { 
       wayPoint.setEle(Double.parseDouble(reading)); 
      } catch (Exception e) { 
       System.out.println("Error " + e.getMessage()); 
      } 
     } else if (qName.equals("trk")) { 
      tracks.add(track); 
     } else if (qName.equals("time") && tracks.size() == 0) { 
      System.out.println("Time GPX " + reading); 
      fechaExtraido = LocalDateTime.parse(reading, parser); 
     } 

    } 

    @Override 
    public void characters(char[] ch, int start, int length) throws SAXException { 
     reading = new String(ch, start, length); 
    } 

    public List<SAXTrack> getTracks() { 
     return tracks; 
    } 

    public void setTracks(List<SAXTrack> tracks) { 
     this.tracks = tracks; 
    } 

    public List<SAXWayPoint> getWaypoints() { 
     return waypoints; 
    } 

    public void setWaypoints(List<SAXWayPoint> waypoints) { 
     this.waypoints = waypoints; 
    } 

    public List<SAXTrackPoint> getTrackpoints() { 
     return trackpoints; 
    } 

    public void setTrackpoints(List<SAXTrackPoint> trackpoints) { 
     this.trackpoints = trackpoints; 
    } 

    public SAXTrack getTrack() { 
     return track; 
    } 

    public void setTrack(SAXTrack track) { 
     this.track = track; 
    } 

    public SAXTrackPoint getTrackPoint() { 
     return trackPoint; 
    } 

    public void setTrackPoint(SAXTrackPoint trackPoint) { 
     this.trackPoint = trackPoint; 
    } 

    public SAXWayPoint getWayPoint() { 
     return wayPoint; 
    } 

    public void setWayPoint(SAXWayPoint wayPoint) { 
     this.wayPoint = wayPoint; 
    } 

    public LocalDateTime getFechaExtraido() { 
     return fechaExtraido; 
    } 

    public void setFechaExtraido(LocalDateTime fechaExtraido) { 
     this.fechaExtraido = fechaExtraido; 
    } 

    public LocalDateTime getFechaIni() { 
     return fechaIni; 
    } 

    public void setFechaIni(LocalDateTime fechaIni) { 
     this.fechaIni = fechaIni; 
    } 

    public LocalDateTime getFechaFin() { 
     return fechaFin; 
    } 

    public void setFechaFin(LocalDateTime fechaFin) { 
     this.fechaFin = fechaFin; 
    } 

    public DateTimeFormatter getParser() { 
     return parser; 
    } 

    public void setParser(DateTimeFormatter parser) { 
     this.parser = parser; 
    } 

} 
+1

を働きました。 –

+0

これはどんなgpxファイルでも起こります。 5 gpxファイルでテストしているgpxファイルは必要ありません。私にはdownvoteする必要はありません –

答えて

1

です。

あなたの日時インスタンスのすべてが型であると宣言されていますLocalDateTimeので、彼らはテキスト値が従うことparse関数に渡さ期待します:2011-12-03T10:15:30:あるDateTimeFormatter.ISO_LOCAL_DATE_TIME形式を。

日時2007-12-03T10など ISO-8601カレンダーシステムにおけるタイムゾーン無し:15:30 LocalDateTimeのJavaDocから

形式が含まれないことに注意してください:'T'が、ませが含まれていない:'Z'。これはあなたの質問に含まれているすべてのエラーメッセージと一致します。これらのエラーメッセージで参照されるすべてのテキスト値には、'Z'文字が含まれています。

フォーマットパターンを調整してDateTimeFormatter.ofPatternに変更して'Z'文字を削除すると、問題は解決されると思います。

+0

私はしばらくの間、私はまだ不完全な日付を取得している時間を解析していないときにチェックします。私が日付のsysをして、日付の部分文字列を取得する場合 –

+0

日付のおかげで、それは私が持っていた別の問題を解決しましたが、それは私の現在の問題とは無関係です。 –