2

嗨,我有一个这样定义的架构:

<complexType name="x">
    <sequence>
        <element name="year" type="date"/>
                <choice>
                  <element name="comuneNascita" type="string" nillable="true"/>
                  <element name="statoNascita" type="string" nillable="true"/>
                </choice>
    </sequence>
</complexType>

当我尝试编组使用 xjc (使用 xjc:simple 选项)生成的类时,我得到了以下结果:

  [...]
  <statoNascita xsi:nil="true"/>
  <comuneNascita>xxx</comuneNascita>
  [...]

这是完全错误的!

删除 nillable="true" 解决了这个问题,但是我必须指定一个有效的元素(不是 nilled )。任何解决方法?

4

1 回答 1

0

您可以通过对属性进行如下注释来避免您的问题:

@XmlElements({
   @XmlElement(name="comuneNascita", type=String.class),
   @XmlElement(name="statoNascita", type=String.class),
})

您可以让 XJC 使用 JAXB 绑定文件生成如上注释的属性:

<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
          version="2.1">
    <globalBindings choiceContentProperty="true"/>
</bindings> 

了解更多信息

于 2011-05-11T20:50:22.313 回答