如何定制JAXB如何生成复数方法名称?
我们使用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>
生成的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; }
更新
Blaise回答了这个问题。 但是,我更喜欢不在XML模式中混合关注如JAXB自定义。 因此,对于那些具有相同优先级的人来说,这里是一个JAXB绑定文件,它实现了与Blaise所build议的相同的function,从而使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>
默认情况下,为您的模式片段生成以下内容:
import java.util.ArrayList; import java.util.List; 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 = "physicianList", propOrder = { "physician" }) public class PhysicianList { @XmlElement(name = "Physician") protected List<Physician> physician; public List<Physician> getPhysician() { if (physician == null) { physician = new ArrayList<Physician>(); } return this.physician; } }
如果你注释你的XML模式:
<xs:schema xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" jaxb:version="2.1"> <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:annotation> <xs:appinfo> <jaxb:property name="physicians"/> </xs:appinfo> </xs:annotation> </xs:element> </xs:sequence> </xs:complexType> </xs:schema>
然后你可以生成所需的类:
import java.util.ArrayList; import java.util.List; 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 = "physicianList", propOrder = { "physicians" }) public class PhysicianList { @XmlElement(name = "Physician") protected List<Physician> physicians; public List<Physician> getPhysicians() { if (physicians == null) { physicians = new ArrayList<Physician>(); } return this.physicians; } }
也许有点晚回答,但还有另一种方法来简单地生成复数名称, 而无需混合 XML模式和JAXB绑定。
通过使用带有“-extension”模式的JAXB XJC绑定编译器。 需要添加自定义绑定文件,如下所示:
<?xml version="1.0"?> <jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jxb:extensionBindingPrefixes="xjc"> <jxb:globalBindings> <xjc:simple/> </jxb:globalBindings> </jxb:bindings>
参考文献
- 实验更简单,更好的绑定模式 ( 编辑:过时的链接)
- 实验更简单,更好的绑定模式 (新链接)
- JAXB:如何在XSD中指定attrtypes时更改XJC生成的类名称?