我有一个XSD(我无法改变),它包含在complexType的sequence元素中定义的这些元素:
<xsd:element minOccurs="0" name="precision" nillable="true">
<xsd:complexType>
...
</xsd:complexType>
</xsd:element>
<xsd:element minOccurs="0" name="Precision" nillable="true">
<xsd:complexType>
...
</xsd:complexType>
</xsd:element>
当我在模式上运行xjc时,我在ObjectFactory和嵌套类中的名称冲突上获得了异常。
为了尝试解决这个问题,我添加了这个外部绑定,用于将字段映射到唯一名称和类到唯一名称:
<jaxb:bindings node="//*[local-name()='element' and @name='precision']" schemaLocation="schema.xsd">
<jaxb:property name="precision1"/>
<jaxb:class name="precision1"/>
</jaxb:bindings>
<jaxb:bindings node="//*[local-name()='element' and @name='Precision']" schemaLocation="schema.xsd">
<jaxb:property name="Precision2"/>
<jaxb:class name="Precision2"/>
</jaxb:bindings>
这样就可以生成JAXB类,但是这些类的生成方式如下:
@XmlElementRef(name = "precision1", namespace = "namespace.CustomerInvoice2", type = JAXBElement.class, required = false)
protected JAXBElement<CustomerOrderDate.Precision1> precision1;
protected final static QName NAME = new QName("namespace.CustomerInvoice3", "precision");
public Precision11(CustomerOrderDate.Precision11 .Type value) {
super(NAME, ((Class) CustomerOrderDate.Precision11 .Type.class), CustomerOrderDate.class, value);
}
public Precision11() {
super(NAME, ((Class) CustomerOrderDate.Precision11 .Type.class), CustomerOrderDate.class, null);
}
/**
* whole bunch of comments...
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"precision",
"use"
})
public static class Type {
//The actual fields
但是,如果我手动将字段名称更改为precision1和Precision2,则类生成为:
public static class Precision1 {
@XmlElementRef(name = "precision", namespace = "namespace.CustomerInvoice2", type = JAXBElement.class, required = false)
protected JAXBElement<Integer> precision;
@XmlElementRef(name = "use", namespace = "namespace.CustomerInvoice2", type = JAXBElement.class, required = false)
protected JAXBElement<String> use;
@XmlAttribute(name = "mark")
protected String mark;
@XmlAttribute(name = "mode")
protected String mode;
@XmlAttribute(name = "invalid")
protected Boolean invalid;
// blah, blah, blah...
我很好奇是否有可以使用的绑定将QName值保存在根类的字段声明中,并将Type信息保留在生成的类中。
答案 0 :(得分:2)
基于this answer和教程JAXB - Java Architecture for XML Binding。
你只需要:
<jaxb:bindings node="//xs:element[@name='precision']/xs:complexType" schemaLocation="schema.xsd">
<jaxb:class name="Precision1"/>
</jaxb:bindings>
<jaxb:bindings node="//xs:element[@name='Precision']/xs:complexType" schemaLocation="schema.xsd">
<jaxb:class name="Precision2"/>
</jaxb:bindings>