我们使用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) 运行以下xjc命令会引发错误:
$ xjc "ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd"
parsing a schema...
compiling a schema...
[ERROR] Two declarations cause a collision in the ObjectFactory class.
line 340 of ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd
[ERROR] (Related to above error) This is the other declaration.
line 475 of ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd
Run Code Online (Sandbox Code Playgroud)
虽然我理解JAXB绑定以及XJC中的冲突是什么,但我不明白当前模式中的冲突在哪里.
我应该怎么解决这个问题?
谢谢,
皮埃尔
更新:这里是错误的上下文:
$ curl -s "ftp://ftp.ncbi.nih.gov/bioproject/Schema/Core.xsd" | sed 's/^[ \t]*//' | cat -n | egrep -w -A 10 -B 10 '(340|475)'
330 <xs:element maxOccurs="1" name="Description"
331 type="xs:string" minOccurs="0">
332 <xs:annotation>
333 <xs:documentation>
334 Optionally provide description especially …Run Code Online (Sandbox Code Playgroud)