jenetics / jpx

JPX - Java GPX library
Apache License 2.0
206 stars 31 forks source link

JPX 3.1.0 cannot extract TrackPointExtension #179

Closed Nymerea closed 12 months ago

Nymerea commented 12 months ago

Here is an example of gpx file generated by amazfit device, it contains extension like speed or heart rate.

When using 3.1.0 version of jpx, the extension cannot be parsed.

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<gpx xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns3="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:ns2="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:ns1="http://www.cluetrust.com/XML/GPXDATA/1/0" creator="Zepp App" version="8.0.2-play">
    <trk>
        <name><![CDATA[20230811 splitvicte-national-parc]]></name>
        <trkseg>
            <trkpt lat="44.873146" lon="15.599844">
                <ele>622.95</ele>
                <time>2023-08-11T10:25:52Z</time>
                <extensions>
                    <ns3:TrackPointExtension>
                        <ns3:speed>0.11750881</ns3:speed>
                        <ns3:cad>0.0</ns3:cad>
                        <ns3:hr>88</ns3:hr>
                    </ns3:TrackPointExtension>
                </extensions>
                  </trkpt>
        </trkseg>
    </trk>
</gpx>
jenetics commented 12 months ago

Hi, I tried to reproduce it, but can't.

var gpxString = """
    <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
    <gpx xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
        xmlns="http://www.topografix.com/GPX/1/1"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:ns3="http://www.garmin.com/xmlschemas/TrackPointExtension/v1"
        xmlns:ns2="http://www.garmin.com/xmlschemas/GpxExtensions/v3"
        xmlns:ns1="http://www.cluetrust.com/XML/GPXDATA/1/0"
        creator="Zepp App"
        version="8.0.2-play"
    >
        <trk>
            <name><![CDATA[20230811 splitvicte-national-parc]]></name>
            <trkseg>
                <trkpt lat="44.873146" lon="15.599844">
                    <ele>622.95</ele>
                    <time>2023-08-11T10:25:52Z</time>
                    <extensions>
                        <ns3:TrackPointExtension>
                            <ns3:speed>0.11750881</ns3:speed>
                            <ns3:cad>0.0</ns3:cad>
                            <ns3:hr>88</ns3:hr>
                        </ns3:TrackPointExtension>
                    </extensions>
                </trkpt>
            </trkseg>
        </trk>
    </gpx>
    """;

final GPX gpx  = GPX.Reader.of(Mode.LENIENT).fromString(gpxString);
final Document extensions = gpx
    .getTracks().get(0)
    .getSegments().get(0)
    .getPoints().get(0)
    .getExtensions().orElseThrow();

final NodeList trackPointExtension = extensions
    .getDocumentElement()
    .getFirstChild()
    .getChildNodes();

final String speed = trackPointExtension.item(0).getTextContent();
final String cad = trackPointExtension.item(1).getTextContent();
final String hr = trackPointExtension.item(2).getTextContent();

System.out.println("Speed: " + speed);
System.out.println("Cad: " + cad);
System.out.println("HR: " + hr);

The test above will produce the expected result of

Speed: 0.11750881
Cad: 0.0
HR: 88
Nymerea commented 12 months ago

That's my mistake because I saw [#document: null] in memory, I taught it was not parsable.

Sorry for the lose of time.