3

I'm using JAXB and intellij webservices plugin to create java files from XSDs. I have two XSDs that defining the same object but when I create them using the "generate java code from XML schema" the object is created twice with his own package. I already tried with import xsd and using the ref attribute and I get the same result.

Here is an example:

This is the first XSD

<?xml version="1.0" encoding="UTF-8"?>
   <xs:schema targetNamespace="http://www.msp-gs.com/workflow"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
       xmlns:wc="http://www.example.com/workflow"
       attributeFormDefault="unqualified"
       elementFormDefault="qualified"
       jaxb:version="1.0">

    <xs:annotation>
    <xs:appinfo>
        <jaxb:globalBindings enableJavaNamingConventions="true">

        </jaxb:globalBindings>
    </xs:appinfo>
    </xs:annotation>

    <xs:element name="WC">

    <xs:complexType>

        <xs:sequence>

            <xs:element name="Example"
                        type="wc:Restriction"
                        minOccurs="1"
                        maxOccurs="unbounded">

            </xs:element>

        </xs:sequence>

        </xs:complexType>

    </xs:element>

    <xs:complexType name="Restriction">

        <xs:attribute type="xs:string"
                  name="authorizationTreeId"/>

    </xs:complexType>

    </xs:schema>

This is the second XSD

    <?xml version="1.0" encoding="UTF-8"?>
     <xs:schema targetNamespace="http://www.msp-gs.com/workflow"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
       xmlns:fd="http://www.example.com/workflow"
       attributeFormDefault="unqualified"
       elementFormDefault="qualified"
       jaxb:version="1.0">

      <xs:annotation>
        <xs:appinfo>
        <jaxb:globalBindings enableJavaNamingConventions="true">

        </jaxb:globalBindings>
    </xs:appinfo>
      </xs:annotation>

    <xs:element name="FD">

        <xs:complexType>

        <xs:sequence>

            <xs:element name="Example"
                        type="fd:Restriction"
                        minOccurs="1"
                        maxOccurs="unbounded">

            </xs:element>

        </xs:sequence>

    </xs:complexType>

    </xs:element>

    <xs:complexType name="Restriction">

    <xs:attribute type="xs:string"
                  name="authorizationTreeId"/>

    </xs:complexType>

     </xs:schema>

I want that Restriction will be the same object.

Thank you.

Rotem
  • 2,306
  • 3
  • 26
  • 44
  • 1
    Check out the following article: http://blog.bdoughan.com/2011/12/reusing-generated-jaxb-classes.html – bdoughan Jan 02 '12 at 13:26
  • 1
    Thank you, I will get into it right now and let you know if it's working. Thank you again. – Rotem Jan 02 '12 at 13:59
  • @Rotem: What stops you from using one XSD file in both cases? The problem with two XSDs is that you will have to generate model objects twice, but what you can is to use the same output directory for that, so the 2nd run will override the 1st. Or you can perform some tricks after the schema generation (copy / clean). – dma_k Jan 02 '12 at 14:43
  • @dma_k: First of all the output directory should be separated because there is a logical separation (apples and oranges). Secondly, I can't copy / clean after generation because I also need to marshal and unmarshal those generated java files and I don't know if the process will be affected by these actions. – Rotem Jan 02 '12 at 15:09
  • You want to kill two rabbits: have separate XSDs and something in common. This is not directly possible. If tomatoes are something common for apples and oranges, you extract them into separate project, generate model into the same package as for oranges and apples and remove tomatoes (e.g. using ant rule) from oranges and apples projects. – dma_k Jan 02 '12 at 15:32
  • You may have a point here but it's too late to do those kind of changes in our project. I'm still working on @BlaiseDoughan solution. – Rotem Jan 02 '12 at 16:58
  • Well I manage to do this using @BlaiseDoughan solution. Thank you again. – Rotem Jan 02 '12 at 17:43
  • @Rotem - I added an official answer since the approach I recommended worked for you. – bdoughan Jan 02 '12 at 18:31

1 Answers1

3

You can tell JAXB to use an existing Java class instead of generating one using an external binding file like the following. In the example below we are telling JAXB to use the existing Product class:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bindings version="2.1" xmlns="http://java.sun.com/xml/ns/jaxb">
  <bindings scd="x-schema::tns"
    xmlns:tns="http://www.example.org/Product">
    <schemaBindings map="false"/>
    <bindings scd="tns:product">
      <class ref="org.example.product.Product"/>
    </bindings>
  </bindings>
</bindings>

If you are using the XJC tool to generate classes from an XML schema, you can use the -episode flag to have XJC generate a binding file pointing to all the classes that it generated. This will allow you to reuse previously generated classes.

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

For More Information

bdoughan
  • 147,609
  • 23
  • 300
  • 400