I wish to convert several schemas into Java Code. The schemas are all similar; for example, each one has a TXLife root object. It would be easier to manage the code if each schema generated code with unique class-names. I can use the "package" binding to put the code from each schema into its own package, but when I try to use the "prefix" binding to change the class names, it ignores it.
Here is my schema_bindings.xml file:
<?xml version="1.0" encoding="UTF-8"?> <jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xsd="http://www.w3.org/2001/XMLSchema" jaxb:version="2.0"> <jaxb:bindings schemaLocation="schemas/HI_Request.xsd" node="/xsd:schema"> <jaxb:schemaBindings> <jaxb:package name="com.mycompany.hi"/> <jaxb:nameXmlTransform> <jaxb:typeName prefix="Hi_"/> </jaxb:nameXmlTransform> </jaxb:schemaBindings> </jaxb:bindings> </jaxb:bindings>
When I run the xjc command I get (I had to modify the classpath inside the xjc.bat file in order to get it to work):
C:\test>\progs\Java\jaxb-ri-2.2.7\bin\xjc.bat -extension -d src -b schema_bindings.xml schemas
parsing a schema... compiling a schema... com\mycompany\hi\Holding.java com\mycompany\hi\InquiryLevel.java com\mycompany\hi\KeyedValue.java com\mycompany\hi\OLifE.java com\mycompany\hi\ObjectFactory.java com\mycompany\hi\Policy.java com\mycompany\hi\TXLife.java com\mycompany\hi\TXLifeRequest.java com\mycompany\hi\TransMode.java com\mycompany\hi\TransSubType.java com\mycompany\hi\TransType.java
What I was hoping for is that each java file (and the class inside) would be named "Hi_<name>". Jaxb seems to be completely ignoring my "prefix" specification. I have tried several variations on the bindings file. I have also tried the same bindings using the Ant xjc task, all with the same results.
I can work with these results, but it would mean that code that processes input from one schema and produces output to another schema would have to use fully-qualified class names to refer to the objects, which is awkward.
TL;DR
<jaxb:typeName prefix="Hi_"/>
corresponds to the classes generated from named complex types. You could do the following by adding<jaxb:elementName prefix="Hi_"/>
to affect the classes generated from global elements:Full Example
Below is a complete example.
schema.xsd
The schema below has a global element and a named complex type.
binding.xml
XJC Call
Output
We see that the class corresponding to the global element was prefixed with
Element_
and the class corresponding to the named complex type was prefixed withType
.ObjectFactory
andpackage-info
are not part of the domain model and are leveraged by JAXB for metadata so their names were not affected.