jaxb – 如何在xjc中禁用Java命名约定?

例如,xsd中的sOmE_PROPerty必须是 java类中的sOmE_PROPerty而不是someProperty.

我试图使用globalBindings enableJavaNamingConventions =“false”但它不起作用.

解决方法

您将需要使用underscoreBinding =“asCharInWord”而不是enableJavaNamingConventions =“false”:

customer.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema 
    targetNamespace="http://www.example.org/customer" 
    xmlns="http://www.example.org/customer"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified"> 

    <xsd:complexType name="customer">
                <xsd:sequence>
                    <xsd:element name="sOmE_PROPerty" type="xsd:string"/>
                </xsd:sequence>
    </xsd:complexType>

</xsd:schema>

binding.xml

JAXB绑定文件用于自定义Java转换的模式:

<jaxb:bindings 
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">
    <jaxb:globalBindings underscoreBinding="asCharInWord"/>
</jaxb:bindings>

XJC电话

xjc -d out -b binding.xml customer.xsd

顾客

生成属性名称现在包含下划线字符:

package org.example.customer;

import javax.xml.bind.annotation.XmlAccesstype;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccesstype.FIELD)
@XmlType(name = "customer",propOrder = {
    "sOmEPROPerty"
})
public class Customer {

    @XmlElement(name = "sOmE_PROPerty",required = true)
    protected String sOmEPROPerty;

    public String getSOmE_property() {
        return sOmEPROPerty;
    }

    public void setSOmE_PROPerty(String value) {
        this.somEPROPerty = value;
    }

}

不使用binding.xml

如果您改为进行以下XJC调用

xjc -d out -customer.xsd

您将看到生成属性不包含下划线:

package org.example.customer;

import javax.xml.bind.annotation.XmlAccesstype;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccesstype.FIELD)
@XmlType(name = "customer",required = true)
    protected String sOmEPROPerty;

    public String getSOmEproperty() {
        return sOmEPROPerty;
    }

    public void setSOmEPROPerty(String value) {
        this.somEPROPerty = value;
    }

}

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...