例如,xsd 中的 sOmE_PROPerty 必须是 java 类中的 sOmE_PROPerty 而不是 someProperty。
我尝试使用 globalBindings enableJavaNamingConventions="false" 但它不起作用。
您将需要使用underscoreBinding="asCharInWord"
而不是enableJavaNamingConventions="false"
:
客户.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>
绑定.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", propOrder = {
"sOmEPROPerty"
})
public class Customer {
@XmlElement(name = "sOmE_PROPerty", required = true)
protected String sOmEPROPerty;
public String getSOmEPROPerty() {
return sOmEPROPerty;
}
public void setSOmEPROPerty(String value) {
this.sOmEPROPerty = value;
}
}
通过更改类 com.sun.xml.bind.api.impl.NameConverter 中的 jaxb 的源代码来解决,如下所示:
public static final NameConverter standard = new Standard();
static class Standard extends NameUtil implements NameConverter {
public String toClassName(String s) {
return s;//toMixedCaseName(toWordList(s), true);
}
public String toVariableName(String s) {
return s;//toMixedCaseName(toWordList(s), false);
}
public String toInterfaceName( String token ) {
return token;//toClassName(token);
}
public String toPropertyName(String s) {
String prop = s;//toClassName(s);
// property name "Class" with collide with Object.getClass,
// so escape this.
if(prop.equals("Class"))
prop = "Clazz";
return prop;
}