gpt4 book ai didi

java - 如何将attributeGroup与JAXB绑定(bind)?

转载 作者:行者123 更新时间:2023-12-01 04:31:28 26 4
gpt4 key购买 nike

我正在尝试使用 JAXB 基于一些 xsd 文件生成 java 类。我遇到了一个我自己无法解决的问题。我只是想强调我不能修改xsd 文件。所以这就是我的问题:有 2 个 xsd 文件 MultiDestScheduleRQ 和 MultiDestScheduleRS,它们都有相似的结构:

<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>

MultiDestScheduleRS 中的唯一区别是元素的名称。当我尝试生成 java 类时出现错误:

org.xml.sax.SAXParseException: 'ResponseGroup' is already defined

我尝试使用自定义绑定(bind)修复它:

<jxb:bindings schemaLocation="./../Validated/MultiDestScheduleRQ.xsd" >
<jxb:bindings node="//xs:attributeGroup[@name='ResponseGroup']//xs:attributeGroup">
<jxb:property name="ResponseGroupRQ"/>
</jxb:bindings>
</jxb:bindings>

但它只改变了错误消息

com.sun.istack.SAXParseException2: XPath evaluation of "//xs:attributeGroup[@name='ResponseGroup']//xs:attributeGroup" results in empty target node 

我也尝试过

<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>

最佳答案

很难从 XML 架构片段中看出您已发布的错误来自何处。我将其稍微刷新了一下,并从中成功生成了类:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/schema" xmlns="http://www.example.org/schema"
elementFormDefault="qualified">

<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>

<xs:simpleType name="StringLength1to128">
<xs:restriction base="xs:string" />
</xs:simpleType>

<xs:complexType name="QueryType" />

<xs:simpleType name="Type">
<xs:restriction base="xs:string" />
</xs:simpleType>

</xs:schema>
<小时/>

JAXB 实现不会为属性组生成属性,因此您定义的绑定(bind)文件将不起作用。我在下面提供了一个小示例,演示如何处理属性组。

XML 架构

下面是一个 XML 架构示例,其中包含引用同一属性组的两种复杂类型。

schema.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/schema"
xmlns:tns="http://www.example.org/schema"
elementFormDefault="qualified">

<attributeGroup name="my-attribute-group">
<attribute name="att1" type="string"/>
<attribute name="att2" type="int"/>
</attributeGroup>

<complexType name="foo">
<attributeGroup ref="tns:my-attribute-group"/>
<attribute name="foo-att" type="string"/>
</complexType>

<complexType name="bar">
<attributeGroup ref="tns:my-attribute-group"/>
<attribute name="bar-att" type="string"/>
</complexType>

</schema>

生成的模型

在下面生成的类中,我们看到属性组中的属性的处理方式与复杂类型中定义的属性相同。

@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;

}

关于java - 如何将attributeGroup与JAXB绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17940074/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com