Jaxb的行为很奇怪

Jaxb的行为很奇怪,jaxb,marshalling,unmarshalling,gpx,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

我正在尝试使用扩展名对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>

52.048584
2009-06-19T10:13:04Z
164
99
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();
JAXBContext JAXBContext=JAXBContext.newInstance(GpxType.class,com.cluetrust.xml.gpxdata.\u 1.\u 0.ObjectFactory.class);
Unmarshaller Unmarshaller=jaxbContext.createUnmarshaller();
File File=新文件(“src/test/resources/gpx example.gpx”);
JAXBElement unmarshal=(JAXBElement)unmarshaller.unmarshal(文件);
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());

            }
ExtensionsType extensions=gpx.getExtensions();
List extensionsAny=extensions.getAny();
for(对象对象:extensionsAny){
System.out.println(object.getClass());
if(JAXBElement的对象实例){
JAXBElement元素=(JAXBElement)对象;
LapType LapType=(LapType)元素.getValue();
System.out.println(lapType.getStartTime());
}
似乎我必须在这里跳过一些障碍。我在github上设置了一个非常小的项目,如图所示。我正在使用的完整示例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>

Lap用于包含有关单个活动圈的信息。
根据设备的不同,这可能包含各种附加信息。
根据设备的不同,这可能包含在跑步或课程中。
以及:


内部列表中圈的索引。
以Lat/Long为单位的圈起点
以Lat/Long表示的圈终点
一圈的开始时间
圈的总运行时间(以秒为单位)
跑一圈时燃烧的卡路里数
圈中覆盖的距离(m)
对应于该圈的轨道参考信息
总结不同绩效衡量指标的绩效总结要素,如cadence、hr等。
搭接的触发器。在某些设备上,基于设备已知的属性,搭接可以是手动或自动的。
圈的强度(静止或活动)
这只是该模式引入的众多类型中的一种


我做错了什么?

您是从XML模式生成对象的吗?如果是,XML模式中与您发现的模型奇怪部分相对应的部分是什么样子的?我过去是生成对象的。我会将相关部分添加到原始帖子中
<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>