1

Say I have three schemas: main.xsd, common1.xsd, and common2.xsd.

common1 and common2 already have their own predefined packages containing jaxb objects.

main.xsd imports both common1.xsd and common2.xsd.

When I attempt to generate the jaxb objects for main.xsd, it of course generates all of the objects it refers to via common1 and common2, but to the package main which leads to my problem. Now, when I attempt to set data to an element from main containing references to common1 or common2 in the java code, I of course get the error that common1.element does not match main.element.

Ex) In my java code:

common1.ObjectFactory.getExData() will return common.ExData. main.setExData(ExDataType exampleData) will expect ExDataType from the package main. But I get this data from the ObjectFactory in common1, so it refers to common1.ExDataType

My question is, how do I generate these objects for main in a way that I don't duplicate types created in the main package and they instead refer to the existing common1 or common2 objects?

EDIT
Episode files seemed to be the way to go, so I attempted this route. I first ran the command to generate the episode files for each of my imported schemas using the command supplied by Blaise's answer(but with my filenames/paths substituted):

xjc -d out -episode product.episode Product.xsd

Then I attempted to run the command to generate the JAXB classes for the schema containing these imports using the next command:

xjc -d out ProductPurchaseRequest.xsd -extension -b product.episode

The first command seemed to just generate all of the class files for the "product.xsd" to where I specified in the out parameter, and I don't see an "product.episode" file anywhere. The second command created a new package for each schema it referenced, then created all of the object classes for them in each, but they all contained the wrong package reference and it was quiet messy.

What am I missing in how I ran these commands?

JWiley
  • 3,129
  • 8
  • 41
  • 66

1 Answers1

2

Below are a couple approaches you can use:

Use External Bindings File to Specify a Complex Type Corresponds to an Existing Class

Below is an example bindings file that associates the complex type named Foo with the existing class com.example.Foo:

<jxb:bindings schemaLocation="yourSchema.xsd">
    <jxb:bindings node="//xs:complexType[@name='Foo']">
        <jxb:class ref="com.example.Foo"/>
    </jxb:bindings>
</jxb:bindings>

Use Episode Files

The above process is quite labour intensive if you have a lot of classes. Instead you can leverage episode files. Episode files can be produced as you generate a class model from an XML Schema. Then when you generate classes from another XML Schema that references the first you can point at the episode file to prevent regenerating the classes.

Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • 1
    Hi Blaise- thanks for the response. Episode files look like what I need. I can reference as many episode files as necessary when generating JAXB classes for a schema, correct? – JWiley Feb 06 '14 at 13:44
  • @JWiley - Yes you can have as many episode files as necessary. They are really just external regular JAXB external binding files. Below is help text for the `-b` flag used to specify config files: `specify external bindings files (each must have its own -b); if a directory is given, **/*.xjb is searched` – bdoughan Feb 06 '14 at 14:31
  • Actually I'll probably open a different question, as it pertains to some of the issues I'm having implementing the solution. I've tried both an external binding file and episode, with mixed results. Thanks for the help Blaise! – JWiley Feb 07 '14 at 18:39