如何将attributeGroup与JAXB绑定?

时间:2013-07-30 06:48:37

标签: java binding jaxb

我正在尝试使用JAXB基于某些xsd文件生成java类。我遇到了一个我自己无法解决的问题。我只想强调一下我无法修改xsd文件。 所以这就是我的问题: 有两个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

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

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

1 个答案:

答案 0 :(得分:0)

很难从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实现不会为属性组生成属性,因此您定义的绑定文件将不起作用。我在下面列出了一个小例子,演示如何处理属性组。

XML Schema

下面是一个示例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;

}