Reputation: 399
I'm facing a strange issue with xjc
, trying to map element in a java property. I would like to have on Test2 getter and setter for test3.
I setup my bindings as it:
<?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>
And my xsd is similar to this one:
<?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>
when I'm trying to generate with xjc -b binding.xjb -d out test.xsd
im getting:
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.
I created a github project you can test.
Upvotes: 2
Views: 3076
Reputation: 5320
First, you need to fix up your XSD so that the definitions are associated with a targetNamespace and the element ref is correct.
Try:
<?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>
But then you still have the harder problem of how to pick out the TEST3 child elements with a 'test3' getter among the rest of the mixed content. JAXB is going to map them all into a single List.
From looking at other posts like this it looks like the JAXB2 Simplify plugin might be able to help with your use case.
(Maybe someone will write an even better answer showing how in more detail, but this is probably helpful enough that I'm posting as-is).
Upvotes: 2