我想使用 maven-jaxb2-plugin 生成类。
我的输入 xsd 如下所示:
<complexType>
<complexContent>
<restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
<sequence>
<element name="ReferencedElementA" type="{http://www.w3.org/2001/XMLSchema}IDREFS" minOccurs="0" />
<element name="ReferencedElementB" type="{http://www.w3.org/2001/XMLSchema}IDREF"
maxOccurs="unbounded" minOccurs="0" />
</sequence>
</restriction>
</complexContent>
请注意,我无法更改架构,因为它是由外部客户提供的!
这是生成的代码:
@XmlList
@XmlElement(name = "ReferencedElementA")
@XmlIDREF
@XmlSchemaType(name = "IDREFS")
protected List<Object> referencedElementA;
@XmlElementRef(name = "ReferencedElementB", namespace = "ABC", type = JAXBElement.class, required = false)
protected List<JAXBElement<Object>> referencedElementB;
如您所见,“ReferencedElementB”生成为带有@XMLElementRef-Annotation 的List>。
我想要的是如下所示:
@XmlElement(name = "ReferencedElementB")
@XmlIDREF
@XmlSchemaType(name = "IDREF")
protected List<Object> referencedElementB;
使用Blaise Doughan的解决方法,我使用以下代码扩展了绑定文件:
<bindings node = ".//xs:element[@name='ReferencedElementB']">
<class ref="java.lang.Object"/>
</bindings>
这几乎完成了工作,生成如下代码:
@XmlElement(name = "ReferencedElementB")
@XmlSchemaType(name = "IDREF")
protected List<Object> referencedElementB;
但是,@XmlIDREF-Annotation 仍然缺失。这会在编组期间产生错误。
我试图通过 annox-plugin 注入缺少的注释:
<bindings node = ".//xs:element[@name='ReferencedElementB']">
<class ref="java.lang.Object"/>
<annox:annotate target="field">@javax.xml.bind.annotation.XmlIDREF</annox:annotate>
</bindings>
但是一代失败了
compiler was unable to honor this annox:annotate customization. It is attached to a wrong place, or its inconsistent with other bindings
再一次,我无法更改输入 xsd。
先感谢您!