×

Warning

The mail function has been disabled by an administrator.

When you are working with JAXB, you have three different entry points. Either you create a whole new project, you create an xml schema from existing classes in a normal Java project or vice versa.

In this article I describe how to create a new JAXB project in Eclipse.

If you have not installed the already prepared Eclipse version for Java EE, below you can find an article on how to install the needed software into a standard Java SE Eclipse version.

Setup Eclipse for JAXB

In Eclipse select File -> New -> Other and type "jaxb" into the filter field of the "New" wizard.

You are offered three options.

  • JAXB Classes from Schema
  • JAXB Project
  • Schema for JAXB Classes

Select JAXB Project and configure the new project as shown in schreenshot below.

Click Next and also select Next for the following screen for creating directory structure in the new project.

If you are using Java 1.8 you propbaly will face the error message "The configured runtime is insufficient to provide an implementation for JAXB 2.2.".

The workaround for Java 1.8 is to set the Type dropdown option to "Disable Library Configuration".

However, according to the workarounds warning message this workaround leaves you with the responsibility for the classpath. Since we are not talking deployment for example via ANT scripts this warning can be ignored for an example project like ours.

But hold on, I still have an older JDK installed, 1.7. Let me go back three screens and select the Target Runtime "jdk1.7.0_75". Click Next again until we see the screen showing a warning for classpath configuration and select "JRE" under Type.

Et voila. No warnings. It is up to you if you want to try the example with Java 1.8. I am sticking with 1.7 for now.

Click "Finish" and you'll see ... well not much besides an empty project. The JXB magic is secretly hiding in the new projects configuration. Right click on the project and display its properties.

Here you will find a new top level entry calles JAXB. If you select it, it gives you the option to configure the JAXB Platform as well as the JAXB Implementation Type. Whatever this may be.

Now we are ready to start working with XML Schemas and JAXB.

Create JAXB classes from an XML Schema (*.xsd)

In the src directory of the new project jsut created place a new package called "xml.resource".

In this new project create a new XSD schema definition. To do so hit Ctrl. + N on the xml.resource package and type "schema" into the filter filed of the "New" wizard.

Select "XML Schema File".

Hit Next and provide a name like "HelloWorld.xsd" for the new schema definition.

Click "Finish" and the result should look as follwos.

1
2
3
4
5
6
7
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
	targetNamespace="http://www.example.org/HelloWorld" 
	xmlns:tns="http://www.example.org/HelloWorld" 
	elementFormDefault="qualified">
	
</schema>

For a little test I borrowed the following XML from the following tutorial provided by oracle.

Replace the file contents of HelloWorld.xsd with the following schema definition.

https://jaxb.java.net/tutorial/section_1_3-Hello-World.html#Hello%20World

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
            jxb:version="2.0">
 
<xsd:element name="Greetings" type="GreetingListType"/>
 
<xsd:complexType name="GreetingListType">
  <xsd:sequence>
    <xsd:element name="Greeting" type="GreetingType"
                 maxOccurs="unbounded"/>
  </xsd:sequence>
</xsd:complexType>
 
<xsd:complexType name="GreetingType">
  <xsd:sequence>
    <xsd:element name="Text" type="xsd:string"/>
  </xsd:sequence>
  <xsd:attribute name="language" type="xsd:language"/>
</xsd:complexType>
 
</xsd:schema>

From the context menu of the xsd file select "Generate" > "JAXB Classes ..."

 Then provide the target project as well as the target package you want to create the new classes in.

 

 

After that hit "Next" again and select the option to classify the source file to generate classes from as XML schema.

Click "Finish" and review the reults in package xml. The API has created three new classes in the target package.

An ObjectFactory, which is always created by JAXB and assists in creation of Objects. It also reflects which objects as per XML schema can serve as root objects, but this is something for later.

Two new classes GreetingListType and GreetingType.

In summary

JAXB, I like! It makes creation of high quality XML parsers very easy and comfortable and allows me to focus on the business logic.

Now it's eleven o clock on a Friday and I had half a bottle of vine ... I think I am of to bed. Enjoy and hope it helps a bit.