考虑以下绑定(片段):
<jaxb:bindings node="//xsd:element[@name='CONDITION']/xsd:complexType/xsd:sequence/xsd:element[@name='OPERAND'][position()=1]">
<jaxb:property name="firstOperand"/>
</jaxb:bindings>
<jaxb:bindings node="//xsd:element[@name='CONDITION']/xsd:complexType/xsd:sequence/xsd:element[@name='OPERAND'][position()=2]">
<jaxb:property name="secondOperand"/>
</jaxb:bindings>
以下XML架构(摘录):
<xsd:element name="CONDITION">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="OPERAND" type="OPERANDType"/>
<xsd:element name="OPERATOR" type="xsd:string" />
<xsd:element name="OPERAND" type="OPERANDType" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="OPERANDType">
<xsd:choice>
<xsd:element name="SPECIALCONSTANT" type="xsd:string" />
<xsd:element name="CONSTANT" type="xsd:string" />
</xsd:choice>
</xsd:complexType>
以下输入:
<OPERAND>
<CONSTANT>Test1</CONSTANT>
</OPERAND><OPERATOR>myOperator</OPERATOR>
<OPERAND>
<CONSTANT>Test2</CONSTANT>
</OPERAND>
有人可以解释为什么“getSecondOperand”返回null,为什么“getFirstOperand”实际上包含CONSTANT值为“Test2”?
使用:
- JAXB 2.2.4u1
- Java 1.6.0_23
- Apache Maven 3.0.1
- maven-jaxb2-plugin version 0.8.0
编辑:JAXB生成(为访问者/ mutator删除了JavaDoc:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="OPERAND" type="{}OPERANDType"/>
* <element name="OPERATOR" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="OPERAND" type="{}OPERANDType"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"firstOperand",
"operator",
"secondOperand"
})
@XmlRootElement(name = "CONDITION")
public class CONDITION {
@XmlElement(name = "OPERAND", required = true)
protected OPERANDType firstOperand;
@XmlElement(name = "OPERATOR", required = true)
protected String operator;
@XmlElement(name = "OPERAND", required = true)
protected OPERANDType secondOperand;
public OPERANDType getFirstOperand() {
return firstOperand;
}
public void setFirstOperand(OPERANDType value) {
this.firstOperand = value;
}
public String getOPERATOR() {
return operator;
}
public void setOPERATOR(String value) {
this.operator = value;
}
public OPERANDType getSecondOperand() {
return secondOperand;
}
public void setSecondOperand(OPERANDType value) {
this.secondOperand = value;
}
}
答案 0 :(得分:2)
您的绑定导致XJC生成无效的代码。您最终将两个Java字段绑定到名为OPERAND
的元素,因为Java类中的字段没有隐式的定义顺序(源代码顺序没有任何意义),绑定到的数据每个领域都是不可预测的。
默认情况下,我假设XJC生成了绑定到List
元素的Java CONDITION
。由于缺乏Java的表现力,这是唯一能够在绑定传入XML时真正起作用的配置。
我建议您使用此方法,并在运行时使用您自己的代码解开操作符和操作数,或者尝试使用MOXy这样的非标准JAXB实现,它具有增强的执行此类操作的功能。的事情。