Java 如何将attributeGroup与JAXB绑定?
我正在尝试使用JAXB根据一些xsd文件生成java类。我遇到了一个我自己解决不了的问题。我只想强调我不能修改xsd文件。 这就是我的问题: MultiDestScheduleRQ和MultidestScheduler共有2个xsd文件,它们的结构相似:Java 如何将attributeGroup与JAXB绑定?,java,binding,jaxb,Java,Binding,Jaxb,我正在尝试使用JAXB根据一些xsd文件生成java类。我遇到了一个我自己解决不了的问题。我只想强调我不能修改xsd文件。 这就是我的问题: MultiDestScheduleRQ和MultidestScheduler共有2个xsd文件,它们的结构相似: <xs:attributeGroup name="ResponseGroup"> <xs:annotation> <xs:documentation source="Description"
<xs:attributeGroup name="ResponseGroup">
<xs:annotation>
<xs:documentation source="Description" xml:lang="en">BLA</xs:documentation>
</xs:annotation>
<xs:attribute name="MoreIndicator" type="xs:boolean" use="optional">
<xs:annotation>
<xs:documentation source="Description" xml:lang="en">BLA BLA</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="MoreDataEchoToken" type="StringLength1to128" use="optional">
<xs:annotation>
<xs:documentation source="Description" xml:lang="en">BLA BLA BLA.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:attributeGroup>
<xs:element name="MultiDestScheduleRQ">
<xs:complexType>
<xs:complexContent>
<xs:extension base="QueryType">
<xs:sequence>
<xs:element name="Journeys">
<xs:complexType>
<xs:sequence>
<xs:element name="Journey" maxOccurs="12">
<xs:complexType>
<xs:attribute name="RPH" type="Type" use="required">
<xs:annotation>
<xs:documentation>BLA</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attributeGroup ref="ResponseGroup"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
我尝试使用自定义绑定修复它:
<jxb:bindings schemaLocation="./../Validated/MultiDestScheduleRQ.xsd" >
<jxb:bindings node="//xs:attributeGroup[@name='ResponseGroup']//xs:attributeGroup">
<jxb:property name="ResponseGroupRQ"/>
</jxb:bindings>
</jxb:bindings>
我也试过了
<jxb:bindings schemaLocation="./../Validated/MultiDestScheduleRQ.xsd" >
<jxb:bindings node="//xs:attributeGroup[@name='ResponseGroup']">
<jxb:property name="ResponseGroupRQ"/>
</jxb:bindings>
</jxb:bindings>
仅再次获取第一个错误(表示“ResponseGroup”已定义的错误)
有人能帮忙吗
更新
我使用maven插件生成类,它是pom的片段
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.7.4</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<verbose>true</verbose>
<schemaDirectory>./src/main/xsd</schemaDirectory>
<schemaIncludes>
<include>**/*/*.xsd</include>
</schemaIncludes>
<bindingDirectory>./src/main/xsd/541_Grammar_Multidest/xjb</bindingDirectory>
<bindingIncludes>
<include>binding.xml</include>
</bindingIncludes>
<generatePackage>com.my.package</generatePackage>
<extension>true</extension>
<staleFile>${project.build.directory}/jaxb2/.schema2XjcStaleFlag</staleFile>
</configuration>
</plugin>
org.jvnet.jaxb2.maven2
maven-jaxb2-plugin
0.7.4
生成
真的
./src/main/xsd
**/*/*.xsd
./src/main/xsd/541_Grammar_Multidest/xjb
binding.xml
com.my.package
真的
${project.build.directory}/jaxb2/.schema2XjcStaleFlag
从您发布的XML模式片段很难判断错误来自何处。我把它冲洗了一点,并从中成功地生成了一个班级:
布拉
布拉
布拉
布拉
等等。
布拉
JAXB实现不会为属性组生成属性,因此您定义的绑定文件将无法工作。下面我提供了一个小示例,演示如何处理属性组 XML模式 下面是一个示例XML模式,其中包含两个引用同一属性组的复杂类型 schema.xsd
生成模型
在下面生成的类中,我们看到属性组中的属性与复杂类型中定义的属性的处理方式相同
Foo
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "foo")
public class Foo {
@XmlAttribute(name = "foo-att")
protected String fooAtt;
@XmlAttribute(name = "att1")
protected String att1;
@XmlAttribute(name = "att2")
protected Integer att2;
}
栏
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "bar")
public class Bar {
@XmlAttribute(name = "bar-att")
protected String barAtt;
@XmlAttribute(name = "att1")
protected String att1;
@XmlAttribute(name = "att2")
protected Integer att2;
}
从您发布的XML模式片段很难判断错误来自何处。我把它冲洗了一点,并从中成功地生成了一个班级:
布拉
布拉
布拉
布拉
等等。
布拉
JAXB实现不会为属性组生成属性,因此您定义的绑定文件将无法工作。下面我提供了一个小示例,演示如何处理属性组 XML模式 下面是一个示例XML模式,其中包含两个引用同一属性组的复杂类型 schema.xsd
生成模型
在下面生成的类中,我们看到属性组中的属性与复杂类型中定义的属性的处理方式相同
Foo
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "foo")
public class Foo {
@XmlAttribute(name = "foo-att")
protected String fooAtt;
@XmlAttribute(name = "att1")
protected String att1;
@XmlAttribute(name = "att2")
protected Integer att2;
}
栏
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "bar")
public class Bar {
@XmlAttribute(name = "bar-att")
protected String barAtt;
@XmlAttribute(name = "att1")
protected String att1;
@XmlAttribute(name = "att2")
protected Integer att2;
}
谢谢你的回答,但也许我没有解释清楚,或者你误解了。问题是我有两个非常相似的xsd文件:MultiDestScheduleRQ和multidestScheduler,它们中都定义了attributeGroup ResponseGroup。这就是我的问题所在。这不是因为两者使用了相同的属性组,而是因为两者都定义了相同的属性组。这就是我试图在binding.xml中重命名其中一个的原因。@homar-一个xml模式导入另一个吗?@homar-那我就糊涂了。如果您的两个XML模式通过导入/包含彼此不相关,则从每个模式生成的类不会冲突