开发者

JAXB Error, sample xsd docs

开发者 https://www.devze.com 2023-02-12 11:27 出处:网络
I\'m getting a JAXB errors when I run \"xjc -p foo.bar bmw.xsd\": [ERROR] A class/interface with the same name \"foo.bar.Fault\" is already in use.

I'm getting a JAXB errors when I run "xjc -p foo.bar bmw.xsd":

[ERROR] A class/interface with the same name "foo.bar.Fault" is already in use. line 16 of bmw.xsd

[ERROR] (Relevant to above error) another one is generated from here. line 26 of abc.xsd

Is it related to the two 'fault' elements cla开发者_如何转开发shing? If so, what do I do to fix?

bmw.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"  elementFormDefault="qualified"    targetNamespace="http://foo.com/bmw" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bmw="http://foo.com/bmw">
<xs:import namespace="http://schemas.xmlsoap.org/soap/envelope/"  schemaLocation="abc.xsd"/>
<xs:element name="rule">
<xs:complexType>
  <xs:sequence>
    <xs:element ref="bmw:customer"/>
    <xs:element ref="bmw:schemaName"/>
    <xs:element ref="bmw:schemaVersion"/>
  </xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="customer" type="xs:integer"/>
<xs:element name="schemaName" type="xs:NCName"/>
<xs:element name="schemaVersion" type="xs:decimal"/>
<xs:element name="fault">
<xs:complexType>
  <xs:sequence>
    <xs:element ref="bmw:faultcode"/>
    <xs:element ref="bmw:faultstring"/>
  </xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="faultcode" type="xs:integer"/>
<xs:element name="faultstring" type="xs:string"/>
</xs:schema>

abc.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://schemas.xmlsoap.org/soap/envelope/" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bmw="http://foo.com/bbs">
<xs:import namespace="http://foo.com/bmw" schemaLocation="bmw.xsd"/>
<xs:element name="Envelope">
<xs:complexType>
  <xs:sequence>
    <xs:element ref="env:Header"/>
    <xs:element ref="env:Body"/>
  </xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Header">
<xs:complexType>
  <xs:sequence>
    <xs:element ref="bmw:rule"/>
  </xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Body">
<xs:complexType>
  <xs:sequence>
    <xs:element ref="env:Fault"/>
  </xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Fault">
<xs:complexType>
  <xs:sequence>
    <xs:element ref="bmw:fault"/>
  </xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>


You have a couple of options when resolving the name conflict.

Option #1 - Schema Annotation

You can annotate the XML schema to resolve the name conflict:

<xs:element name="Fault">
    <xs:annotation>
        <xs:appinfo>
            <jaxb:class name="Fault2" />
        </xs:appinfo>
    </xs:annotation>
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="bmw:fault"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

abc.xsd

Refer to the schema annotation on the "Fault" element.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified" targetNamespace="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bmw="http://foo.com/bbs"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1">
    <xs:import namespace="http://foo.com/bmw" schemaLocation="bmw.xsd" />
    <xs:element name="Envelope">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="env:Header" />
                <xs:element ref="env:Body" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="Header">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="bmw:rule"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="Body">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="env:Fault" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="Fault">
        <xs:annotation>
            <xs:appinfo>
                <jaxb:class name="Fault2" />
            </xs:appinfo>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="bmw:fault"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Option #2 - External Bindings File

Instead of modifying the XML schema you can also use an external bindings file:

bindings.xml

<jaxb:bindings 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">

    <jaxb:bindings schemaLocation="abc.xsd">
            <jaxb:bindings node="//xs:element[@name='Fault']">
                <jaxb:class name="Fault2"/>
            </jaxb:bindings>
    </jaxb:bindings>

</jaxb:bindings>

The xjc command would be:

xjc -d out -b bindings.xml abc.xsd

Package Name

To control the package name you can either pass it as a parameter to the XJC command:

xjc -d out -b bindings.xml -p com.foo.bar abc.xsd

Or amend the bindings file.


Yes, your error is related to the clash. You can fix this with a customized binding. A customized binding is a way of telling JAXB to do something other than what it naturally would do. Try looking into the class tag in an external binding file if you cannot annotate the XSD yourself.

0

精彩评论

暂无评论...
验证码 换一张
取 消