【问题标题】:JAXB Fixed Attribute not set during marshalling编组期间未设置 JAXB 固定属性
【发布时间】:2015-10-09 06:28:19
【问题描述】:

默认情况下,JAXB 似乎无法设置固定的属性值。这是预期的行为,还是我做错了什么?

我有一个 xsd 喜欢:

<element name="AccountCategory" type="tns:Integer"></element>
<xs:complexType name="Integer">
    <xs:simpleContent>
        <xs:extension base="xs:int">
            <xs:attribute name="e-dtype" fixed="int"/>
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>

编组使用 new 生成的 java 对象:

<AccountCategory>5</AccountCategory>

Java:

com.sample.Integer val = new com.sample.Integer();
val.setValue(5);
parentObject.setAccountCategory(val);

我可以手动设置属性值,它工作正常。此外,如果我只是将其重置为自己的值,它也可以工作。好像 marshaller 在生成 XML 时没有使用 get 方法?

val.setEDtype(val.getEDtype());

结果

<AccountCategory e-dtype="int">5</AccountCategory>

生成的.java如下:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Integer", propOrder = {
    "value"
})
public class Integer {

@XmlValue
protected int value;
@XmlAttribute(name = "e-dtype")
@XmlSchemaType(name = "anySimpleType")
protected String eDtype;

/**
 * Gets the value of the value property.
 * 
 */
public int getValue() {
    return value;
}

/**
 * Sets the value of the value property.
 * 
 */
public void setValue(int value) {
    this.value = value;
}

/**
 * Gets the value of the eDtype property.
 * 
 * @return
 *     possible object is
 *     {@link String }
 *     
 */
public String getEDtype() {
    if (eDtype == null) {
        return "int";
    } else {
        return eDtype;
    }
}

/**
 * Sets the value of the eDtype property.
 * 
 * @param value
 *     allowed object is
 *     {@link String }
 *     
 */
public void setEDtype(String value) {
    this.eDtype = value;
}

【问题讨论】:

    标签: java xml xsd jaxb


    【解决方案1】:

    引用XML Schema Part 0 - 2.2.1 Occurrence Constraints:

    fixed 属性用于属性和元素声明中,以确保将属性和元素设置为特定值。例如,po.xsd 包含country 属性的声明,该属性声明为fixedUS。这个声明意味着country属性在实例文档中的出现是可选的(use的默认值是optional),虽然如果属性确实出现,它的值必须是US,如果属性未出现,架构处理器将提供一个 country 属性,其值为 US

    因此,如您所见,因为您的属性是 optional,除非您提供值,否则不会生成它,但该值必须是 int 才能符合架构。

    调用get 将为您提供默认/固定值,应该如此
    如果未设置,则生成将省略属性,应该

    不,编组器没有使用get 方法,因为@XmlAccessorTypeXmlAccessType.FIELD

    【讨论】:

    • OK 是有道理的。但是,如果我更新 xsd 以使属性所需的相同行为仍然存在。还有什么我想念的吗?
    • 是的,您错过了 XML generation 不强制 XML 架构这一事实,因此您可以生成不符合架构的 XML,包括出现约束(useminOccursmaxOccurs)。请参阅javadoc 中的“验证和格式良好”部分
    • 谢谢。我能够使用 fixedAttributeAsConstantProperty xjc 绑定来解决问题。
    【解决方案2】:

    尝试使用 jaxb 绑定 fixedAttributeAsConstantProperty,例如 here

    <schema targetNamespace="https://stackoverflow.com/example" 
            xmlns="http://www.w3.org/2001/XMLSchema" 
            xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
            xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
            jaxb:version="2.0">
      <annotation>
        <appinfo>
          <jaxb:globalBindings fixedAttributeAsConstantProperty="true" />
        </appinfo>
      </annotation>
      ...
    </schema>
    

    【讨论】:

      猜你喜欢
      • 2012-05-29
      • 2014-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多