Jaxb的行为很奇怪

时间:2015-04-25 15:53:53

标签: jaxb marshalling unmarshalling gpx

我正在尝试使用扩展程序对gpx文件进行编组和解组。 gpx架构允许在extensions标记示例中添加扩展名:

<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>

GPX文件是正确的,并通过这样的解组:

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();
对我来说,有两件事情很奇怪。首先,我必须解组JAXBElement而不能直接使用GpxType,我必须首先转到JAXBElement。

其次,一旦我拥有GpxType对象,那就是(对我来说是最大的问题)。部分确实完成了,但扩展不是:

ExtensionsType extensions = gpx.getExtensions();
        List<Object> extensionsAny = extensions.getAny();
        for (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());

            }

看来我必须在这里跳过一些箍。我在github上设置了一个非常小的项目,其中显示了jaxbproblem。我正在使用的完整示例GPX文件也在那里。其中的命名空间是正确的:

<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">

编辑: 架构中的相关部分是:

<xsd:element name="lap" type="lapType">
    <xsd:annotation>
        <xsd:documentation>
            Lap is used to contain information about an individual lap of activity.
            Depending upon the device, this may contain a variety of additional information.
            Depending upon the device, this may be contained within a run or course.
        </xsd:documentation>
    </xsd:annotation>
</xsd:element>

<xsd:complexType name="lapType">
    <xsd:sequence>
        <xsd:element name="index" type="xsd:int" minOccurs="0" maxOccurs="1">
            <xsd:annotation><xsd:documentation>The index of the lap in the 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 the 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 the 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 the 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 the 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 the 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 the lap</xsd:documentation></xsd:annotation>
        </xsd:element>
        <xsd:element name="trackReference" type="trackReferenceType" minOccurs="0" maxOccurs="1">
            <xsd:annotation><xsd:documentation>Reference information for the track which corresponds to this 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 as 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 the lap.  On some devices, the lap may be manual or automatic based on attributes known by the device.</xsd:documentation></xsd:annotation>
        </xsd:element>
        <xsd:element name="intensity" type="intensityKind" minOccurs="0" maxOccurs="1">
            <xsd:annotation><xsd:documentation>The intensity of the lap (whether resting or active)</xsd:documentation></xsd:annotation>
        </xsd:element>
    </xsd:sequence>
</xsd:complexType>

这只是可以找到此架构的众多类型之一here

我做错了什么?

0 个答案:

没有答案