SAX로 gpx 파일의 날짜를 파싱하는 데 문제가 있습니다. 어떻게 든 그것은 일부 노드의 <time>
태그를 올바르게 얻지 못합니다. 대부분의 노드는 괜찮습니다. 다른 태그에는 문제가없는 것으로 보입니다.
다른 gpx 파일에서 테스트 중이며 동일한 파일의 동일한 노드에서 항상 실패하며 특수 문자 또는 기타 문자가 없습니다. 그리고 그것은 주기적입니다.SAX Java로 날짜 구문 분석하기
나는 오류 옆에있는 노드 번호를 인쇄하고 있습니다. 그리고 13 번째 열은 전류 오차
그것은 일반적으로 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;
}
}
을했다. –
모든 gpx 파일에서 발생합니다. 5gpx 파일로 테스트중인 gpx 파일이 필요 없습니다. 나를 downvote 할 필요가 없다. –