Quick Table of Contents
JAXB - Java Architecture for XML Binding
1. What is it used for
I've used this as an XML to Java serializer / deserializer for web services. Alternatives to this include XMLBeans
2. Creating Java from XML
Use xjc that comes in the bin directory. It will be calling com.sun.tools.xjc.XJCFacade.main or via an ant task or maven plugin. See https://jaxb.dev.java.net/jaxb20-ea/docs/xjcTask.html for info on parameters to xjc.
xjb - binding file
One file that you might need to use, is the xjb file if the auto generate doesn't work properly.
Troubleshooting
xjc created duplicate classes and won't compile
This happens if the xsd has elements that nest elements of the same name
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://magicmonster.com/test/duplicates" xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="ErrorTest">
<xs:complexType>
<xs:sequence>
<xs:element name="ErrorTest">
<xs:complexType>
<xs:sequence>
<xs:element name="Hello"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
produces
..
@XmlRootElement(name = "ErrorTest")
public class ErrorTest {
..
..
public static class ErrorTest {
..
Which does not compile due to 'Duplicate Class: ErrorTest'. Static inner classes can't have the same name as the parent class.
Solution to this is to create a xjb mapping to tell xjc how to transform the elements. You are control many things, including the java class names
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb
http://java.sun.com/xml/ns/jaxb/bindingschema_1_0.xsd"
>
<jxb:bindings schemaLocation="example.xsd"
node="//xs:element[@name='ErrorTest']//xs:element[@name='ErrorTest']/xs:complexType">
<jxb:class name="ErrorTestXXX"/>
</jxb:bindings>
</jxb:bindings>
Note that it matches 'xs:complexType', not 'xs:element'
3. Project Home
JAXB home page is at https://jaxb.dev.java.net/.
| More Articles (showing 2 below) | |
|---|---|
| 2011-06-18 | |
| 2010-07-26 | |

