2012-01-29 74 views
1

考虑以下绑定(片段):JAXB 2个绑定工作不正常

<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”实际上是con保持“Test2”的CONSTANT值?

使用:
- JAXB 2.2.4u1
- 的Java 1.6.0_23
- 阿帕奇Maven的3.0.1
- Maven的JAXB2插件版本0.8.0

编辑:JAXB生成( Java文档的存取/存取器取出。

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> 
* &lt;complexType> 
* &lt;complexContent> 
*  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*  &lt;sequence> 
*   &lt;element name="OPERAND" type="{}OPERANDType"/> 
*   &lt;element name="OPERATOR" type="{http://www.w3.org/2001/XMLSchema}string"/> 
*   &lt;element name="OPERAND" type="{}OPERANDType"/> 
*  &lt;/sequence> 
*  &lt;/restriction> 
* &lt;/complexContent> 
* &lt;/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

看到绑定并没有帮助。我们需要看到生成的代码。 – skaffman 2012-01-29 23:34:18

+0

@skaffman:添加了生成的代码。让我知道如果还有什么可以帮助。 – AndrewBourgeois 2012-01-30 07:51:08

回答

2

你绑定造成XJC生成的代码,将不能使用,你已经结束了与绑定到一个元素两个Java领域称为OPERAND,因为没有给Java类中的字段隐式定义的顺序(源代码顺序没有任何意义),绑定到每个字段的数据将不可预知。

默认情况下,我假设XJC生成了绑定到CONDITION元素的Java List。由于缺乏Java的表现力,这是绑定传入XML的唯一配置。我建议你要么与此一起生活,并在运行时用自己的代码解开运算符和操作数,要么尝试使用非标准的JAXB实现,如MOXy,它具有增强的功能来执行此类操作。

+0

我所做的是基于http://stackoverflow.com/questions/868658/xjc-binding-customization-does-not-work-in-jaxb-2-1-3-it-works-in-jaxb-2 -0。避免名单确实是我想要的。那么这个人在关联问题上做了什么也被打破了? – AndrewBourgeois 2012-01-30 10:40:49

+0

我刚刚删除了这部分的绑定,并像以前一样使用列表,感谢您的帮助! – AndrewBourgeois 2012-01-31 09:12:09