我在尝试映射 java 属性中的元素时遇到了 xjc
的奇怪问题。我想在 Test2 上使用 test3 的 getter 和 setter。
我将我的绑定(bind)设置为:
<?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它看起来像 JAXB2 Simplify plugin也许能够帮助您解决用例。
(也许有人会写一个更好的答案,更详细地说明如何操作,但这可能足够有帮助,我可以按原样发布)。
关于xml - XJC - [错误]编译器无法支持此属性自定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38478348/