2

我正在尝试为以下示例 xsd 生成 JAXB 生成的对象。

<xs:complexType name="AddressType">   
      <xs:sequence>   
        <xs:element name="USA" type="xs:string"/>   
      </xs:sequence>   
   </xs:complexType>  

并且在没有任何自定义绑定的情况下生成的类是

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

    @XmlElement(name = "USA", required = true)   
    protected String usa;   

    /**  
     * Gets the value of the usa property.  
     *   
     * @return  
     *     possible object is  
     *     {@link String }  
     *       
     */  
    public String getUSA() {   
        return usa;   
    }   

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

如您所见,字段名称是“usa”,而 setter/getter 是 getUSA/setUSA。

是否有任何自定义设置/绑定使字段名称也生成为“USA”而不是“usa”,这样字段和属性都是“USA”。

我提到了如何在 JAXB 中自定义属性名称?

但那是自定义属性,而不是字段..任何帮助

顺便说一句,我正在使用 maven-jaxb2-plugin

4

1 回答 1

4

与您的情况一样的示例 xjb 文件:binding.xjb

例子 :

<jaxb:bindings 
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.1">

<jaxb:bindings schemaLocation="schema.xsd">
    <jaxb:bindings node="//xs:element[@name='USA']">
        <jaxb:property name="usa" />
    </jaxb:bindings>
</jaxb:bindings>

</jaxb:bindings>

使用 xjc 命令添加 -b binding.xjb 或在 maven xjc 插件中配置绑定文件位置。

于 2015-03-18T13:04:28.260 回答