+

Search Tips   |   Advanced Search

Use JAXB tools to generate an XML schema file from a Java class


Use Java Architecture for XML Binding (JAXB) tooling to generate an XML schema file from Java classes.

Identify the Java classes or a set of Java objects to map to an XML schema file.

Use JAXB APIs and tools to establish mappings between Java classes and XML schema. XML schema documents 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 bottom-up development approach starting from existing Java Beans or enterprise beans, use the wsgen tool to generate the artifacts for JAX-WS applications. After the Java artifacts for the application are generated, we 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. Java class annotations provide the capability to customize the default mappings from existing Java classes to the generated schema components. The XML schema file along with the annotated Java class files contain all the necessary information that the JAXB runtime requires to parse the XML documents for marshaling and unmarshaling.

We 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. Java class annotations provide the capability to customize the default mappings from existing Java classes to the generated schema components. The XML schema file along with the annotated Java class files contain all the necessary information that the JAXB runtime requires to parse the XML documents for marshaling and unmarshaling.

Supported configurations: The wsimport, wsgen, schemagen and xjc command-line tools are not supported on the z/OS platform. This functionality is provided by the assembly tools provided with WAS running on the z/OS platform. Read about these command-line tools for JAX-WS applications to learn more about these tools.

Best practice: WebSphere provides JAX-WS and JAXB tooling. The wsimport, wsgen, schemagen and xjc command-line tools are located in...

APP_ROOT\bin\

Similar tooling is provided by the Java SE Development Kit (JDK) 6. For the most part, artifacts generated by both the tooling provided with WebSphere and the JDK are the same. In general, artifacts generated by the JDK tools are portable across compliant runtime environments. However, it is a best practice to use the WebSphere tools to achieve seamless integration within the WebSphere environment. bprac

Supported configurations: WAS Version 7.0 supports the JAXB 2.1 specification. JAX-WS 2.1 requires JAXB 2.1 for data binding. sptcfg

JAXB 2.1 provides improvements in compilation support to enable you to configure the schemagen schema generator so that it does not automatically generate a new schema. This is helpful if we are using a common schema such as the World Wide Web Consortium (W3C), XML Schema, WSDL, or WS-Addressing and you do not want a new schema generated for a particular package that is referenced. The location attribute on the @XmlSchema annotation causes the schemagen generator to refer to the URI of the existing schema instead of generating a new one.

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

Avoid trouble: When running the schemagen tool, the schema generator does not correctly read the @XmlSchema annotations from the package-info class file to derive targetNamespaces. Instead of using the @XMLSchema annotation, use one of the following methods:

 

  1. Locate the Java source files or Java class files to use in generating an XML schema file. Ensure that all classes referenced by the Java class files are contained in the classpath or are provided to the tool using the-classpath/-cp options.

  2. Use the JAXB schema generator, schemagen command to generate an XML schema. The schema generator is located in...

    APP_ROOT\bin\

    (Windows)

    APP_ROOT\bin\schemagen.bat myObj1.java myObj2.java
    

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

    APP_ROOT/bin/schemagen.sh myObj1.java myObj2.java
    

    The parameters, myObj1.java and myObj2.java, are the names of the Java files containing the data objects. If myObj1.java or myObj2.java refer to Java classes that are not passed into the schemagen command, use the -cp option to provide the classpath location for these Java classes. Read about the schemagen command to learn more about this command and additional options that we can specify.

  3. (Optional) Use JAXB program annotations defined in the javax.xml.bind.annotations package to customize the JAXB XML schema mappings.

  4. (Optional) Set the location property on the @XmlSchema annotation to indicate to the schema compiler to use an existing schema rather than generating a new one. For example,

    @XmlSchema(namespace="foo") package foo;
    @XmlType class Foo {
    @XmlElement Bar zot;
    }
    
    
    

    @XmlSchema(namespace="bar", location="http://example.org/test.xsd") package bar; @XmlType class Bar { ... } <xs:schema targetNamespace="foo"> <xs:import namespace="bar" schemaLocation="http://example.org/test.xsd"/> <xs:complexType name="foo"> <xs:sequence> <xs:element name="zot" type="bar:Bar" xmlns:bar="bar"/> </xs:sequence> </xs:complex

    the location="http://example.org/test.xsd" indicates the location on the existing schema to the schemagen tool and a new schema is not generated.

 

Results

Now that we have generated an XML schema file from Java classes, we are ready to marshal and unmarshal the Java objects as XML instance documents.

Avoid trouble: The schemagen command does not differentiate the XML namespace between multiple @XMLType annotations that have the same @XMLType name defined within different Java packages. When this scenario occurs, the following error is produced:

Error: Two classes have the same XML type name .... Use @XmlType.name and @XmlType.namespace to assign different names to them...
This error indicates we have class names or @XMLType.name values that have the same name, but exist within different Java packages. To prevent this error, add the @XML.Type.namespace class to the existing @XMLType annotation to differentiate between the XML types.

 

Example

The following example illustrates how JAXB tooling can generate an XML schema file from an existing Java class, Bookdata.java.

  1. Copy the following Bookdata.java file to a temporary directory.

    package generated;
     import javax.xml.bind.annotation.XmlAccessType;
     import javax.xml.bind.annotation.XmlAccessorType;
     import javax.xml.bind.annotation.XmlAttribute;
     import javax.xml.bind.annotation.XmlElement;
     import javax.xml.bind.annotation.XmlType;
     import javax.xml.datatype.XMLGregorianCalendar;
    
    
    
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "bookdata", propOrder = {
        "author",     "title",     "genre",     "price",     "publishDate",     "description"
    }) public class Bookdata {
    
        @XmlElement(required = true)
        protected String author;
        @XmlElement(required = true)
        protected String title;
        @XmlElement(required = true)
        protected String genre;
        protected float price;
        @XmlElement(name = "publish_date", required = true)
        protected XMLGregorianCalendar publishDate;
        @XmlElement(required = true)
        protected String description;
        @XmlAttribute
        protected String id;
    
         public String getAuthor() {
            return author;
        }
        public void setAuthor(String value) {
            this.author = value;
        }
        public String getTitle() {
            return title;
        }
    
         public void setTitle(String value) {
            this.title = value;
        }
    
      
        public String getGenre() {
            return genre;
        }
       
        public void setGenre(String value) {
            this.genre = value;
        }
    
        
        public float getPrice() {
            return price;
        }
    
        
        public void setPrice(float value) {
            this.price = value;
        }
    
       
        public XMLGregorianCalendar getPublishDate() {
            return publishDate;
        }
    
        
        public void setPublishDate(XMLGregorianCalendar value) {
            this.publishDate = value;
        }
    
       
        public String getDescription() {
            return description;
        }
    
        
        public void setDescription(String value) {
            this.description = value;
        }
    
       
        public String getId() {
            return id;
        }
    
        
        public void setId(String value) {
            this.id = value;
        }
    
    }
    

  2. Open a command prompt.

  3. Run the schemagen schema generator tool from the directory where you copied the Bookdata.java file.

    (Windows)

    APP_ROOT\bin\schemagen.bat Bookdata.java
    

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

    APP_ROOT/bin/schemagen.sh Bookdata.java 
    

  4. The XML schema file, schema1.xsd is generated:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <xs:schema version="1.0" 
        xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
      <xs:complexType name="bookdata">
        <xs:sequence>
          <xs:element name="author" type="xs:string"/>
          <xs:element name="title" type="xs:string"/>
          <xs:element name="genre" type="xs:string"/>
          <xs:element name="price" type="xs:float"/>
          <xs:element name="publish_date" type="xs:anySimpleType"/>
          <xs:element name="description" type="xs:string"/>
        </xs:sequence>
        <xs:attribute name="id" type="xs:string"/>
      </xs:complexType>
    </xs:schema>
    
    

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


JAXB

 

Related tasks


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

 

Related


schemagen command for JAXB applications
JAX-WS annotations
Web services specifications and APIs

 

Related information


JAXB 2.0 Reference implementation
JAXB specification documentation