Using JAXB schemagen tooling to generate an XML schema file from a Java class
Use Java™ Architecture for XML Binding (JAXB) schemagen tooling to generate an XML schema file from Java classes.
Before you begin
About this task
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, 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.
To develop web services using a bottom-up development approach starting from existing JavaBeans or enterprise beans, use the wsgen tool to generate the artifacts for Java API for XML-Based Web Services (JAX-WS) applications. After the Java artifacts for your application are generated, 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.
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.
支援的配置: z/OS® 平台不支援 wsimport、wsgen、schemagen 和 xjc 指令行工具。
這項功能是由隨執行於 z/OS 平台的 WebSphere® Application Server 提供的組合工具所提供。請閱讀 JAX-WS 應用程式的這些指令行工具以進一步瞭解這些工具。sptcfg
最佳作法: WebSphere Application Server 提供 Java API for XML-Based Web Services (JAX-WS) 和 Java Architecture for XML Binding (JAXB) 工具。
wsimport、wsgen、schemagen 和 xjc 指令行工具位於 WebSphere Application Server 傳統版的 app_server_root\bin\ 目錄中。
xjc 和 schemagen 指令位於 Liberty 設定檔的 app_server_root\jaxb\bin\ 目錄中。
在某些情況下,WebSphere Application Server 和 JDK 提供的工具所產生的構件,支援相同層次的規格。一般而言,JDK 工具所產生的構件可以在其他標準執行時期環境之間轉移。
不過,最好的作法是利用這個產品所提供的工具來實現在 WebSphere Application Server 環境內的緊密整合,以及使用可能只在 WebSphere Application Server 環境內受到支援的特性。
如果要利用 JAX-WS 和 JAXB 2.2 版工具,請使用在 app_server_root\bin\ 目錄內之應用程式伺服器所提供的工具。bprac
支援的配置: This product supports
the JAXB 2.2 specification. JAX-WS 2.2 requires JAXB 2.2 for data
binding. sptcfgJAXB provides 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 you are using a common schema such as the World Wide Web Consortium (W3C), XML Schema, Web Services Description Language (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, you 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. To function properly, this Ant task requires that you invoke Ant using the ws_ant script.
避免困難: 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:- Provide a package-info.java file with the @XmlSchema; for example:
schemagen sample.Address sample\package-info.java - Use the @XmlType annotation namespace attribute to specify a namespace;
for example:
@XmlType(namespace="http://myNameSpace")
Procedure
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.
避免困難: 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.gotchaExample
- 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; } } - Open a command prompt.
- Run the schemagen schema generator tool from the directory where
you copied the Bookdata.java file.
![[Windows]](../images/windows.gif)
app_server_root\bin\schemagen.bat Bookdata.java
![[Linux]](../images/linux.gif)
![[AIX]](../images/aixlogo.gif)
![[HP-UX]](../images/hpux.gif)
![[Solaris]](../images/solaris.gif)
app_server_root/bin/schemagen.sh Bookdata.java
![[IBM i]](../images/iseries.gif)
app_server_root/bin/schemagen Bookdata.java
- 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.

作業主題