+

Search Tips   |   Advanced Search

Use JAXB tools to generate JAXB classes from an XML schema file


Use JAXB tooling to compile an XML schema file into fully annotated Java classes.

Develop or obtain an XML schema file.

Use JAXB APIs and tools to establish mappings between an XML schema and Java classes. XML schemas describe the data elements and relationships in an XML document. After a data mapping or binding exists, we can convert XML documents to and from Java objects. We can now access data stored in an XML document without the need to understand the data structure.

To develop Web services using a top-down development approach starting with an existing WSDL file, use the wsimport tool to generate the artifacts for the JAX-WS applications when starting with a WSDL file. After the Java artifacts for the application are generated, we can generate fully annotated Java classes from an XML schema file by using the JAXB schema compiler, xjc command-line tool. The resulting annotated Java classes contain all the necessary information that the JAXB runtime requires to parse the XML for marshaling and unmarshaling. Use the resulting JAXB classes within JAX-WS applications or other Java applications for processing XML data.

In addition to using the xjc tool from the command-line, we can invoke this JAXB tool from within the Ant build environments. Use the com.sun.tools.xjc.XJCTask Ant task from within the Ant build environment to invoke the xjc schema compiler tool.

 

  1. Use the JAXB schema compiler, xjc command to generate JAXB-annotated Java classes. The schema compiler is located in...

    APP_ROOT\bin\

    The schema compiler produces a set of packages containing Java source files and JAXB property files depending on the binding options used for compilation.

  2. (Optional) Use custom binding declarations to change the default JAXB mappings. Define binding declarations either in the XML schema file or in a separate bindings file. We can pass custom binding files by using the -b option with the xjc command.

  3. Compile the generated JAXB objects. To compile generated artifacts, add the Thin Client for JAX-WS with WAS to the classpath.

 

Results

Now that we have generated JAXB objects, we can write Java applications using the generated JAXB objects and manipulate the XML content through the generated JAXB classes.

 

Example

The following example illustrates how JAXB tooling can generate Java classes when starting with an existing XML schema file.

  1. Copy the following bookSchema.xsd schema file to a temporary directory.

      <xsd:schema 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">                                          
         <xsd:element name="CatalogData">                                                               
           <xsd:complexType >                                                                         
             <xsd:sequence>                                                                         
               <xsd:element name="books" type="bookdata" minOccurs="0" 
                maxOccurs="unbounded"/>    
             </xsd:sequence>                                                                        
           </xsd:complexType>                                                                         
         </xsd:element>                                                                                 
         <xsd:complexType name="bookdata">                                                              
           <xsd:sequence>                                                                             
             <xsd:element name="author" type="xsd:string"/>                                         
             <xsd:element name="title" type="xsd:string"/>                                          
             <xsd:element name="genre" type="xsd:string"/>                                          
             <xsd:element name="price" type="xsd:float"/>                                           
             <xsd:element name="publish_date" type="xsd:dateTime"/>                                 
             <xsd:element name="description" type="xsd:string"/>                                    
           </xsd:sequence>                                                                            
           <xsd:attribute name="id" type="xsd:string"/>                                               
         </xsd:complexType>                                                                             
       </xsd:schema>        
    
    

  2. Open a command prompt.

  3. Run the JAXB schema compiler, xjc command from the directory where the schema file is located. The xjc schema compiler tool is located in...

    APP_ROOT\bin\

    (Windows)

    APP_ROOT\bin\xjc.bat bookSchema.xsd
    

    [Linux] [AIX] [HP-UX] [Solaris]

    APP_ROOT/bin/xjc.sh bookSchema.xsd
    

    Run the xjc command generates the following JAXB Java files:

    generated\Bookdata.java generated\CatalogdData.java generated\ObjectFactory.java
    
    

  4. Use the generated JAXB objects within a Java application to manipulate XML content through the generated JAXB classes.

Refer to the JAXB 2.0 Reference implementation documentation for additional information about the xjc command.


JAXB

 

Related tasks


Use JAXB for XML data binding
Use JAXB tools to generate an XML schema file from a Java class
Use the JAXB runtime to marshal and unmarshal XML documents

 

Related


Web services specifications and APIs

 

Related information


JAXB 2.0 Reference implementation
JAXB 2.0 spec documentation