相关疑难解决方法(0)

如何自定义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>
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)

java xml jaxb jaxb2

39
推荐指数
2
解决办法
3万
查看次数

xjc:两个声明在ObjectFactory类中导致冲突

运行以下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)

java schema binding xsd xjc

20
推荐指数
2
解决办法
4万
查看次数

标签 统计

java ×2

binding ×1

jaxb ×1

jaxb2 ×1

schema ×1

xjc ×1

xml ×1

xsd ×1