JAXB处理XML序列

Age*_*men 13 java jaxb

我正在尝试使用Java 7中提供的JAXB实现来处理一些XML文件.我正在使用这些版本:

501 ~ % xjc -version
xjc 2.2.4
502 ~ %java -version        
java version "1.7.0_01"
Java(TM) SE Runtime Environment (build 1.7.0_01-b08)
Java HotSpot(TM) Server VM (build 21.1-b02, mixed mode)
Run Code Online (Sandbox Code Playgroud)

XML模式中有问题的解码如下:

<xsd:complexType name="CategorizeType">
    <xsd:complexContent>
        <xsd:extension base="se:FunctionType">
            <xsd:sequence>
                <xsd:element ref="se:LookupValue"/>
                <xsd:element ref="se:Value"/>
                <xsd:sequence minOccurs="0" maxOccurs="unbounded">
                    <xsd:element ref="se:Threshold"/>
                    <xsd:element ref="se:Value"/>
                </xsd:sequence>
                <xsd:element ref="se:Extension" minOccurs="0" />
            </xsd:sequence>
            <xsd:attribute name="thresholdBelongsTo"
                  type="se:ThresholdBelongsToType" use="optional"/>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,se中有两个显式出现:Type中的值.但是,它不会停止使用xjc进行编译.如果我查看为此类型生成的Java类,我可以看到在逻辑上可以检索元素

 <xsd:sequence minOccurs="0" maxOccurs="unbounded">
     <xsd:element ref="se:Threshold"/>
     <xsd:element ref="se:Value"/>
 </xsd:sequence>
Run Code Online (Sandbox Code Playgroud)

使用以下方法:

public List<Object> getThresholdAndValue() {
    if (thresholdAndValue == null) {
        thresholdAndValue = new ArrayList<Object>();
    }
    return this.thresholdAndValue;
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,如果我尝试获取列表的元素,我只能检索在我的xml文件中注册为阈值的元素,其中CategorizeType实例定义如下:

 <Categorize thresholdsBelongTo="succeeding" fallbackValue="0">
     <LookupValue>
         <ns3:ValueReference>OUI_EEE92</ns3:ValueReference>
     </LookupValue>
     <Value>0.3</Value>
     <Threshold>30.0</Threshold>
     <Value>0.4</Value>
     <Threshold>40.0</Threshold>
     <Value>0.45</Value>
     <Threshold>45.0</Threshold>
     <Value>0.5</Value>
     <Threshold>50.0</Threshold>
     <Value>0.55</Value>
     <Threshold>55.0</Threshold>
     <Value>0.6</Value>
     <Threshold>60.0</Threshold>
     <Value>0.7</Value>
     <Threshold>70.0</Threshold>
     <Value>0.8</Value>
     <Extension>
         <ExtensionParameter name="method">MANUAL</ExtensionParameter>
     </Extension>
 </Categorize>
Run Code Online (Sandbox Code Playgroud)

检索列表时,我只能看到阈值.

我做错了吗?这是Jaxb的内在限制吗?

请注意,我无法更改XML架构...

编辑:

我刚刚使用-v选项运行xjc,并获得全局相同的输出.冗长:

xjc -verbose se/2.0/All.xsd
parsing a schema...
[WARNING] java.net.SocketException: Unexpected end of file from server
  line 23 of file:/home/alexis/crap/SE-Schema-2.0/ows/2.0/ows19115subset.xsd

[WARNING] java.net.SocketException: Unexpected end of file from server
  line 22 of file:/home/alexis/crap/SE-Schema-2.0/filter/2.0/filterCapabilities.xsd

compiling a schema...
[INFO] generating codee
unknown location
Run Code Online (Sandbox Code Playgroud)

没有它 :

xjc se/2.0/All.xsd 
parsing a schema...
[WARNING] java.net.SocketException: Unexpected end of file from server
  line 23 of file:/home/alexis/crap/SE-Schema-2.0/ows/2.0/ows19115subset.xsd

[WARNING] java.net.SocketException: Unexpected end of file from server
  line 22 of file:/home/alexis/crap/SE-Schema-2.0/ows/2.0/owsExceptionReport.xsd

compiling a schema...
Run Code Online (Sandbox Code Playgroud)

以下输出仅包含生成的文件的名称和位置.

我忘了说这个xsd无法使用Java 6附带的xjc进行编译.最后尝试过使用JAXB 2.1.10.我现在无法重现这种行为,因为我现在正在使用Java 7 .

编辑2:

我刚刚尝试按照评论中的建议自定义binginds.我的绑定文件如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jxb:bindings jxb:version="1.0"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"    >
    <jxb:bindings schemaLocation="schema.xsd" 
                   node="//xsd:complexType[@name='CategorizeType']">
        <jxb:bindings 
                  node="xsd:complexContent/xsd:extension/xsd:sequence/xsd:element[@ref='se:Value'][position()=1]">
            <jxb:property name="FirstValue"/>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>
Run Code Online (Sandbox Code Playgroud)

第一个值实例确实被Java代码中的firstValue属性替换

@XmlElement(name = "Value", required = true)
protected ParameterValueType firstValue;
@XmlElements({
    @XmlElement(name = "Threshold", type = LiteralType.class),
    @XmlElement(name = "Value", type = ParameterValueType.class)
})
protected List<Object> thresholdAndValue;

public ParameterValueType getFirstValue() {
    return firstValue;
}
public void setFirstValue(ParameterValueType value) {
    this.firstValue = value;
}
public List<Object> getThresholdAndValue() {
    if (thresholdAndValue == null) {
        thresholdAndValue = new ArrayList<Object>();
    }
    return this.thresholdAndValue;
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,我仍然得到相同的结果 - 我仍然无法在getThresholdAndValues()返回的列表中看到我的值.我还在探索定制方式......

小智 1

我认为你需要一个complexType元素

            <xsd:sequence minOccurs="0" maxOccurs="unbounded">
                <xsd:element ref="se:Threshold"/>
                <xsd:element ref="se:Value"/>
            </xsd:sequence>
Run Code Online (Sandbox Code Playgroud)

您能否在运行 xjc 之前插入 XSLT 转换以将 ComplexType 添加到架构定义中?