XJC - [ERROR]编译器无法遵循此属性自定义

时间:2016-07-20 10:10:13

标签: xml xsd jaxb xjc

我在xjc面临一个奇怪的问题,试图在java属性中映射元素。我想在Test2 getter和setter for test3。

我设置我的绑定:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">
    <jaxb:globalBindings localScoping="toplevel" underscoreBinding="asCharInWord">
    </jaxb:globalBindings>
    <jaxb:bindings schemaLocation="test.xsd">
        <jaxb:schemaBindings>
            <jaxb:package name="test.detail" />
        </jaxb:schemaBindings>
        <jaxb:bindings node="//xsd:element[@name='TEST']">
            <jaxb:class name="Test"></jaxb:class>
        </jaxb:bindings>
        <jaxb:bindings node="//xsd:element[@name='TEST1']">
            <jaxb:class name="Test1" />
        </jaxb:bindings>
        <jaxb:bindings node="//xsd:element[@name='TEST2']">
            <jaxb:class name="Test2Impl" />
        </jaxb:bindings>
        <jaxb:bindings node="//xsd:element[@name='TEST3']">
            <jaxb:property name="test3" />
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

我的xsd与此类似:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="" elementFormDefault="qualified"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
    <xsd:element name="TEST1">
        <xsd:complexType mixed="true">
            <xsd:sequence>
                <xsd:element name="TEST2" maxOccurs="unbounded">
                    <xsd:complexType mixed="true">
                        <xsd:sequence>
                            <xsd:element name="TEST3" minOccurs="0" type="xsd:string" />
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="TEST">
        <xsd:complexType mixed="true">
            <xsd:sequence>
                <xsd:element ref="TEST1" minOccurs="0" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

当我尝试使用xjc -b binding.xjb -d out test.xsd生成时生成:

parsing a schema...
[WARNING] EmptyTargetNamespace: In schema document 'jaxb-generate-test/test.xsd',
the value of the 'targetNamespace' attribute cannot be an empty string.
  line 3 of jaxb-generate-test/test.xsd

[ERROR] compiler was unable to honor this property customization. 
It is attached to a wrong place, or its inconsistent with other bindings.
  line 20 of jaxb-generate-test/binding.xjb

[ERROR] (the above customization is attached to the following location in the schema)
  line 10 of jaxb-generate-test/test.xsd

Failed to parse a schema.

我创建了一个你可以测试的github project

1 个答案:

答案 0 :(得分:1)

首先,您需要修复XSD,以便定义与targetNamespace相关联,并且元素引用是正确的。

尝试:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://myns" elementFormDefault="qualified"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:tns="http://myns">
    <xsd:element name="TEST1">
        <xsd:complexType mixed="true">
            <xsd:sequence>
                <xsd:element name="TEST2" maxOccurs="unbounded">
                    <xsd:complexType mixed="true">
                        <xsd:sequence>
                            <xsd:element name="TEST3" minOccurs="0" type="xsd:string" />
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="TEST">
        <xsd:complexType mixed="true">
            <xsd:sequence>
                <xsd:element ref="tns:TEST1" minOccurs="0" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

但是你仍然有一个更难的问题,即如何在混合内容的其余部分中使用'test3'getter选择TEST3子元素。 JAXB将把它们全部映射到一个List中。

通过查看this等其他帖子,JAXB2 Simplify plugin似乎可以为您的用例提供帮助。

(也许有人会写一个更好的答案,详细说明如何,但这可能是有用的,我按原样张贴。)