例如,xsd中的sOmE_PROPerty必须是java类中的sOmE_PROPerty而不是someProperty。如何在xjc中禁用Java命名约定?
我试图使用globalBindings enableJavaNamingConventions =“false”,但它不起作用。
例如,xsd中的sOmE_PROPerty必须是java类中的sOmE_PROPerty而不是someProperty。如何在xjc中禁用Java命名约定?
我试图使用globalBindings enableJavaNamingConventions =“false”,但它不起作用。
通过改变JAXB的源代码级的COM解决。 sun.xml.bind.api.impl.NameConverter是这样的:
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;
}
您是否将此作为xjc扩展实现的,还是您从字面上修改了它的源代码? – 2015-02-18 15:49:01
@RobertoBonvallet,你可以实现NameConverter接口,然后用反射修改public static final NameConverter标准字段。 – Smertokogt 2015-03-13 18:33:39
您将需要使用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", 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;
}
}
感谢您的答案,但即使在你的例子它不工作。它只是在getter中,我需要在这里“protected String sOmEPRoperty”。而且,不能将_之后的第一个字母改为小写字母,否则就是大写字母。 – Smertokogt 2011-06-08 08:07:44
感谢您的回答,我确实有同样的问题,我通过您的答案解决了问题。 – 2011-06-09 15:58:42
这适用于xml,但我有一个稍微不同的问题... 我有
@Blaise谢谢你的回答,但即使在你的例子,它不工作。它只是在getter中,我需要在这里“protected String sOmEPRoperty”。并且不能将第一个字母改成小写字母,并且在'_'后改为大写字母。 – Smertokogt 2011-06-08 08:34:08
通过更改类jaxb的源代码解决com.sun.xml.bind.api.impl.NameConverter – Smertokogt 2011-06-12 13:09:34