41

I'm a beginner to JAXB and I'm having annoying issues when generating Java classes with xjc. I am provided with a XSD like this:

<xs:element name="item" type="itemType"/>  
...   
<xs:complexType name="itemType">
    <xs:attribute name="id" type="xs:string" use="required">
    ...     
</xs:complexType>

and xjc is generating a class called ItemType.java, but I want the name to be Item.java. That is, I want the generated classes as if the XSD was like this:

<xs:element name="item">
    <xs:complexType>
    <xs:attribute name="id" type="xs:string" use="required">
        ...
    </xs:complexType>
</xs:element>

There won't be any reuse of itemType on any other element, it's just the people that constructs the XSD likes it this way. I guess there may be a way to do it with custom bindings but I still haven't found how.

Any help?

Thanks, Miguel

skaffman
  • 381,978
  • 94
  • 789
  • 754
miguel perher
  • 821
  • 1
  • 7
  • 16

3 Answers3

70

JAXB provides two ways to accomplish this:

1. Inline Schema Anntotations

You can use JAXB schema annotations to control the class names.

<xs:schema 
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        jaxb:version="2.1">

    <xs:complexType name="itemType">
        <xs:annotation>
            <xs:appinfo>
                <jaxb:class name="Item"/>
            </xs:appinfo>
        </xs:annotation>
        <xs:attribute name="id" type="xs:string" use="required"/>
    </xs:complexType>

</xs:schema>

2. External Binding File

This customization can also be done via and external binding file:

<jxb:bindings 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">

    <jxb:bindings schemaLocation="your-schema.xsd">
            <jxb:bindings node="//xs:complexType[@name='itemType']">
                <jxb:class name="Item"/>
            </jxb:bindings>
    </jxb:bindings>

</jxb:bindings>

The xjc command line would be:

xjc -d out -b binding.xml your-schema.xsd
bdoughan
  • 142,244
  • 22
  • 280
  • 377
  • 1
    Thanks Blaise. I can't change xsd so it must be done with external binding file. By now, I'm doing it as you said, but I would like to do it more generic. That is, if I have an element with 'type' pointing to a complex type, can I manage that the compilation creates a class which name is the name of the element instead of the name of the complex type? – miguel perher Jan 27 '11 at 08:14
  • 3
    what I mean is that if in the future the people that creates the xsd add a new in the xsd, could I reuse that binding file so that it generates Car class automatically, without the need of modifying the binding file? – miguel perher Jan 27 '11 at 08:20
  • 1
    im using the above tags as suggested by Blaise and getting the below error: parsing a schema... [ERROR] compiler was unable to honor this class customization. It is attached to a wrong place, or its inconsistent with other bindings. line 8 of file:/C:/subhasish/demo/src/main/java/demo.xml [ERROR] (the above customization is attached to the following location in the sc hema) line 4 of file:/C:/subhasish/demo/src/main/java/demo.xsd Failed to parse a schema. – Subhasish Sahu Jan 13 '17 at 10:45
  • Hi, xjc and wsdl noob here, can you explain what xs:complexType is and how do I list these? how do I know how is mine called? – Luis Mauricio Feb 19 '21 at 16:48
18

Well, I finally found how to do it. My external binding file is:

<?xml version="1.0"?>
<jxb:bindings version="1.0"
              xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
              xmlns:xs="http://www.w3.org/2001/XMLSchema"
              xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
              jxb:extensionBindingPrefixes="xjc">

  <jxb:globalBindings>              
    <xjc:simple/>
  </jxb:globalBindings>

</jxb:bindings>

and xjc command must be executed with -extension option.

I found the solution in this page:

http://weblogs.java.net/blog/kohsuke/archive/2006/03/why_does_jaxb_p.html (EDIT: obsolete link)

https://community.oracle.com/blogs/kohsuke/2006/03/03/why-does-jaxb-put-xmlrootelement-sometimes-not-always (new link)

Regards, Miguel

opticyclic
  • 5,802
  • 8
  • 46
  • 92
miguel perher
  • 821
  • 1
  • 7
  • 16
1

This is the external binding file I use to rename a complextype. Compiles with cxf's wsdl2java.

<?xml version="1.0" encoding="UTF-8"?>
<jaxws:bindings xmlns:jaxws="http://java.sun.com/xml/ns/jaxws" xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
    <jaxws:bindings node="wsdl:definitions/wsdl:types/xsd:schema[1]">
        <jxb:bindings node="//xs:complexType[@name='VatNumber_exception']">
             <jxb:class name="VatNumException"/>
        </jxb:bindings>

    </jaxws:bindings>
</jaxws:bindings>
nilsmagnus
  • 2,052
  • 18
  • 31