Using 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.

Before you begin

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

About this task

Use JAXB APIs and tools to establish mappings between an 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, you can convert XML documents to and from Java objects. You can now access data stored in an XML document without the need to understand the data structure.

You 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.

Procedure

  1. Locate the annotated Java source files or Java class files to use in generating an XML schema file. Ensure that all classes referenced by your Java class files are contained in the classpath definition 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 the app_server_root\bin\ directory.
    [Windows]
    app_server_root\bin\schemagen.bat myObj1.java myObj2.java
    [Linux] [AIX] [HP-UX] [Solaris]
    app_server_root/bin/schemagen.sh myObj1.java myObj2.java
    where 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, you must use the -cp option to provide the classpath location for these Java classes.
  3. (Optional) Use JAXB program annotations defined in the javax.xml.bind.annotations package to customize the JAXB XML schema mappings.

Results

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

Avoid trouble 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 you 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.gotcha

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_server_root\bin\schemagen.bat Bookdata.java
    [Linux] [AIX] [HP-UX] [Solaris]
    app_server_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 2.0 Reference implementation documentation for additional information about the schemagen command.




In this information ...


Related concepts

IBM Redbooks, demos, education, and more

(Index)

Use IBM Suggests to retrieve related content from ibm.com and beyond, identified for your convenience.

This feature requires Internet access.

Task topic Task topic    

Terms and conditions for information centers | Feedback

Last updatedLast updated: Aug 31, 2013 1:23:07 AM CDT
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=pix&product=was-nd-dist&topic=twbs_jaxbjava2schema
File name: twbs_jaxbjava2schema.html