Alex Rivera | Logout

jaxb override package of a specific generated set of classes

Asked 2012-01-12T16:05:20.933
13

I have a large third party xsd file that pulls in a whole bunch of other xsd files through imports. The whole thing generates over 1000 classes. When I tell the xjc (jaxb) processor to generate everything in a specific package I get all kinds of naming conflicts. If I don't specify a package, then the processor creates java packages using the target attributes in the xsd files, and the whole thing generates without any errors.

The problem is that the package structure is awful because the target names are chosen really poorly. Other developers hate it. They only need a few classes.

So what I've been trying to do is to give the processor a bindings.xml file where I want to specify certain classes to be generated in a given package name. I've gone back and forth between documentation on Oracle's site, forums, samples. I can't quote every single iteration of my bindings file. My current incarnation is this:

<jaxb:bindings 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1"
    schemaLocation="heavy.xsd"
    node="//xsd:element[@name='Error']"
>

    <jaxb:bindings node="//xsd:element[@name='Error']">
        <jaxb:package name="ABC"/>
    </jaxb:bindings>

    <jaxb:globalBindings
        underscoreBinding="asCharInWord"
        localScoping="toplevel"
        typesafeEnumMaxMembers="10000"
        generateElementClass="true"
    >
    </jaxb:globalBindings>


<!--
        <jaxb:package name="ABC"/>
-->

<!--
    <jaxb:schemaBindings>
        <jaxb:package name="ABC"/>
    </jaxb:schemaBindings>
-->

<!--
    <jaxb:schemaBindings>
        <jaxb:package name="ABC"/>
        <jaxb:nameXmlTransform>
            <jaxb:elementName prefix="Error"/>
        </jaxb:nameXmlTransform>
    </jaxb:schemaBindings>
-->

</jaxb:bindings>

No matter wh

Edit
Report

1 Answer

7

The way I would do it is more like this:

<?xml version="1.0" encoding="utf-8"?>
<jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.1" jxb:extensionBindingPrefixes="xjc">
    <jxb:bindings schemaLocation="XSD1.xsd" node="/xs:schema">
        <jxb:globalBindings>
            <!-- -->
        </jxb:globalBindings>
        <jxb:schemaBindings xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">
            <jxb:package name="com.something">
            </jxb:package>
        </jxb:schemaBindings>
        <jxb:bindings node="//xs:element[@name='Error']">
            <jxb:class xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" name="MyError"/>
            <jxb:property xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" name="Whatever"/>
        </jxb:bindings>
    </jxb:bindings>
    <jxb:bindings schemaLocation="XSD2.xsd" node="/xs:schema">
        <jxb:schemaBindings xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">
            <jxb:package name="com.somethingelse">
            </jxb:package>
        </jxb:schemaBindings>
    </jxb:bindings>
</jxb:bindings>
answered 2012-01-12T16:53:56.287

Your Answer