我们使用JAXB生成Java类,并且遇到了生成多个方法名称不正确的一些情况.例如,我们期望getPhysicians得到的地方getPhysicien.我们如何定制JAXB如何将特定方法复数化?
架构:
<xs:complexType name="physician">
<xs:sequence>
...
</xs:sequence>
</xs:complexType>
<xs:complexType name="physicianList">
<xs:sequence>
<xs:element name="Physician"
type="physician"
minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)
生成的Java代码:
...
public class PhysicianList {
...
@XmlElement(name = "Physician")
protected List<Physician> physicien;
...
public List<Physician> getPhysicien() {
if (physicien == null) {
physicien = new ArrayList<Physician>();
}
return this.physicien;
}
Run Code Online (Sandbox Code Playgroud)
更新
Blaise已经回答了这个问题.但是,我不希望在XML模式中混合诸如JAXB自定义之类的问题.所以对于那些具有相同偏好的人来说,这里有一个JAXB绑定文件,它实现了与Blaise建议的相同的东西,使JAXB自定义不受模式的影响:
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.0">
<jaxb:bindings schemaLocation="myschema.xsd">
<jaxb:bindings node="//xs:complexType[@name='physicianList']//xs:element[@name='Physician']">
<jaxb:property name="physicians"/>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
Run Code Online (Sandbox Code Playgroud)