XJC - [错误] 编译器无法接受此 属性 自定义
XJC - [ERROR] compiler was unable to honor this property customization
我遇到 xjc
的奇怪问题,试图映射 java 属性 中的元素。我想在 Test2 getter 和 setter 上为 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你可以测试。
首先,您需要修复 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 将把它们全部映射到一个列表中。
通过查看 this it looks like the JAXB2 Simplify plugin 等其他帖子可能会对您的用例有所帮助。
(也许有人会写出更好的答案,更详细地展示如何,但这可能对我有帮助,所以我按原样发布)。
我遇到 xjc
的奇怪问题,试图映射 java 属性 中的元素。我想在 Test2 getter 和 setter 上为 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你可以测试。
首先,您需要修复 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 将把它们全部映射到一个列表中。
通过查看 this it looks like the JAXB2 Simplify plugin 等其他帖子可能会对您的用例有所帮助。
(也许有人会写出更好的答案,更详细地展示如何,但这可能对我有帮助,所以我按原样发布)。