Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB: How to change XJC-generated classes names when attr type is specified in XSD?

Tags:

types

jaxb

xsd

xjc

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

like image 434
miguel perher Avatar asked Jan 25 '11 12:01

miguel perher


People also ask

How do you use XJC generated classes?

Open a command prompt. Run the JAXB schema compiler, xjc command from the directory where the schema file is located. The xjc schema compiler tool is located in the app_server_root \bin\ directory. Use the generated JAXB objects within a Java application to manipulate XML content through the generated JAXB classes.

What is XJB file in JAXB?

xjb extension to resolve any conflicts in the WSDL or schema. For example if two elements have the same name and you want to distinguish between them you can rename one by specifying it the bindings file.

Which component or tool in JAXB can generate Java files from a XML schema?

You can create an XML schema document from an existing Java application that represents the data elements of a Java application by using the JAXB schema generator, schemagen command-line tool. The JAXB schema generator processes either Java source files or class files.

What is XJC in Java?

XJC is a Java SE tool that compiles an XML schema file into fully annotated Java classes. It is distributed within the JDK package and is located at /bin/xjc path.


2 Answers

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 
like image 118
bdoughan Avatar answered Oct 15 '22 01:10

bdoughan


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

like image 36
miguel perher Avatar answered Oct 15 '22 00:10

miguel perher