Pat
Pat

Reputation: 13

generate java classes from xsd with mutlitlevel imports

I am trying to generate java classes from xsd using JAXB plugin but not able to get the effect as I want. My use case is :

a.xsd has some elements. b.xsd has some elements.

composite.xsd needs to have some elements from "a.xsd" and "b.xsd" as well as it's own elements.

I have tried many options so far. I can import the xsds ( a and b ) into "composite" but that would only enable me to use the elements from "a" and "b" into "composite" xsd but when I generate the classes using jaxb, it won't automatically bring all contents from "a and b". For example :

a.xsd -> has "name" element.
b.xsd -> has "phone" element.
composite.xsd -> imports a and b and has "nickname" element. 

So if I don't explicitly use "name" and "phone" in composite.xsd, generated java class won't generate those. Also there could be multilevel imports ( kind of inheritance like composite.xsd includes "b.xsd" and "b.xsd" includes "a.xsd ).

So I want composite to have all elements from "a" and "b" in generated class without explicitly repeating a.xsd and b.xsd's elements in composite.xsd.

DESIRED OUTPUT:

composite.class
name, phone, nickname. 

Please advise.

Here are some more details with xsd details: ( field names are different that what I put in the original question but will give a gist of it. ).

**a.xsd**
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="Customer">
  <xsd:sequence>
    <xsd:element name="name" type="xsd:string" />
  </xsd:sequence>
</xsd:complexType>
</xsd:schema>

**b.xsd**
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="Payments">
  <xsd:sequence>
    <xsd:element name="amount" type="xsd:float" />
  </xsd:sequence>
</xsd:complexType>
</xsd:schema>


**composite.xsd**

<xsd:include schemaLocation="x.xsd" />
<xsd:include schemaLocation="y.xsd" />

<xsd:complexType name="CustomerPayments">
<xsd:sequence>
  <xsd:element name="customer" type="Customer" />
  <xsd:element name="payments" type="Payments" />
</xsd:sequence>
</xsd:complexType>

</xsd:schema>

WITH above xsds, what I want to achieve is to have a composite java class (generated by JAXB maven plugin ) to automatically have fields like "name" and "amount" from imported/included xsds.

Upvotes: 0

Views: 587

Answers (1)

lexicore
lexicore

Reputation: 43651

The generated CustomerPayments class will not contain propertiesname or amount, this is not how XJC works.

But it will contain field customer and payments of types Customer and Payments which will contain properties name and amount respectively. So you can do customerPayments.getCustomer().getName() at the end.

Upvotes: 0

Related Questions