marshalling - Jaxb behaviour is odd -
i trying marshall , unmarshal gpx files extension. gpx schema allows have extension added in extensions tag example:
<trkpt lat="51.219983" lon="6.765224"> <ele>52.048584</ele> <time>2009-06-19t10:13:04z</time> <extensions> <gpxdata:hr>164</gpxdata:hr> <gpxdata:cadence>99</gpxdata:cadence> </extensions> </trkpt>
the gpx file correct , passes unmarshalling this:
jaxbcontext jaxbcontext = jaxbcontext.newinstance(gpxtype.class, com.cluetrust.xml.gpxdata._1._0.objectfactory.class); unmarshaller unmarshaller = jaxbcontext.createunmarshaller(); file file = new file("src/test/resources/gpx-example.gpx"); jaxbelement<gpxtype> unmarshal = (jaxbelement<gpxtype>) unmarshaller.unmarshal(file); gpxtype gpx = unmarshal.getvalue();
two things me odd. first off have unmarshal jaxbelement , cannot go straight gpxtype, have go jaxbelement first.
second of , (for me biggest problem) once have gpxtype object. part indeed done, extensions not:
extensionstype extensions = gpx.getextensions(); list<object> extensionsany = extensions.getany(); (object object : extensionsany){ system.out.println(object.getclass()); if (object instanceof jaxbelement) { jaxbelement element = (jaxbelement) object; laptype laptype = (laptype) element.getvalue(); system.out.println(laptype.getstarttime()); }
it seems have jump through hoops here. have setup small project on github shown jaxbproblem. complete example gpx file using there too. namespaces in correct:
<gpx xmlns="http://www.topografix.com/gpx/1/1" xmlns:gpxdata="http://www.cluetrust.com/xml/gpxdata/1/0" creator="pytrainer http://sourceforge.net/projects/pytrainer" version="1.1">
edit: relevant part in schema :
<xsd:element name="lap" type="laptype"> <xsd:annotation> <xsd:documentation> lap used contain information individual lap of activity. depending upon device, may contain variety of additional information. depending upon device, may contained within run or course. </xsd:documentation> </xsd:annotation> </xsd:element>
and:
<xsd:complextype name="laptype"> <xsd:sequence> <xsd:element name="index" type="xsd:int" minoccurs="0" maxoccurs="1"> <xsd:annotation><xsd:documentation>the index of lap in internal list.</xsd:documentation></xsd:annotation> </xsd:element> <xsd:element name="startpoint" type="locationtype" minoccurs="0" maxoccurs="1"> <xsd:annotation><xsd:documentation>the starting point of lap in lat/long</xsd:documentation></xsd:annotation> </xsd:element> <xsd:element name="endpoint" type="locationtype" minoccurs="0" maxoccurs="1"> <xsd:annotation><xsd:documentation>the ending point of lap in lat/long</xsd:documentation></xsd:annotation> </xsd:element> <xsd:element name="starttime" type="xsd:datetime" minoccurs="0" maxoccurs="1"> <xsd:annotation><xsd:documentation>the starting time of lap</xsd:documentation></xsd:annotation> </xsd:element> <xsd:element name="elapsedtime" type="xsd:float" minoccurs="0" maxoccurs="1"> <xsd:annotation><xsd:documentation>the total elapsed time of lap in seconds</xsd:documentation></xsd:annotation> </xsd:element> <xsd:element name="calories" type="xsd:nonnegativeinteger" minoccurs="0" maxoccurs="1"> <xsd:annotation><xsd:documentation>the number of calories burned during lap</xsd:documentation></xsd:annotation> </xsd:element> <xsd:element name="distance" type="xsd:float" minoccurs="0" maxoccurs="1"> <xsd:annotation><xsd:documentation>distance (in m) covered during lap</xsd:documentation></xsd:annotation> </xsd:element> <xsd:element name="trackreference" type="trackreferencetype" minoccurs="0" maxoccurs="1"> <xsd:annotation><xsd:documentation>reference information track corresponds lap</xsd:documentation></xsd:annotation> </xsd:element> <xsd:element name="summary" type="summarytype" minoccurs="0" maxoccurs="unbounded"> <xsd:annotation><xsd:documentation>performance summary elements summarizing different performance measurements, such cadence, hr, etc.</xsd:documentation></xsd:annotation> </xsd:element> <xsd:element name="trigger" type="triggertype" minoccurs="0" maxoccurs="1"> <xsd:annotation><xsd:documentation>the trigger of lap. on devices, lap may manual or automatic based on attributes known device.</xsd:documentation></xsd:annotation> </xsd:element> <xsd:element name="intensity" type="intensitykind" minoccurs="0" maxoccurs="1"> <xsd:annotation><xsd:documentation>the intensity of lap (whether resting or active)</xsd:documentation></xsd:annotation> </xsd:element> </xsd:sequence> </xsd:complextype>
this 1 of many types being introduced schema can found here
what doing wrong?
Comments
Post a Comment