Program Design: JAXB generate an XML document
An XML schema (Schema) using XML syntax to express an XML document structure. J2EE developers may need an XML schema XML documents. Java XML binding framework (JAXB) provides a bundled compiler, xjc to XML mode from a generation Java class. JAXB xjc generated by the Java class that represents a different XML schema elements and complex types (complexType). (Through a complex type of attributes and elements within the specified elements to provide an element of the definition). An XML schema with the XML documents from the Java class can be constructed out.
In this tutorial, the author from using JAXB to generate an XML schema in Java class. These will generate a Java class examples of XML documents. This article consists of the following:
1. Pre-installed 2. Overview 3. XMl model generated from the Java class 4. Generated in the Java class from an XML document pre-pre-installed in order to use JAXB from an XML generated in the Java class model, JAXB API class libraries and tools should be xjc There CLASSPATH environment variable. Will be Java Web services development kit (JWSDP) 1.5 into an installation directory. Will be the following. Jar document incorporated CLASSPATH environment variable.
<JWSDP> / Jaxb / lib / jaxb-api.jar
<JWSDP> / Jaxb / lib / jaxb-impl.jar
<JWSDP> / Jaxb / lib / jaxb-libs.jar
<JWSDP> / Jaxb / lib / jaxb-xjc.jar
<JWSDP> / Jwsdp-shared/lib/namespace.jar
<JWSDP> / Jwsdp-shared/lib/jax-qname.jar
<JWSDP> / Jwsdp-shared/lib/relaxngDatatype.jar
<JWSDP> Is Java Web services development kit 1.5 installation directory. To <JWSDP> / jaxb / bin join the PATH environment variable. <JWSDP> / Jaxb / bin directory contains xjc compiler. To <JWSDP> / jwsdp-shared/bin directory into the PATH environment variable. <JWSDP> / Jwsdp-shared/bin setenv directory contains the batch file, it set up a JAVA_HOME, ANT_HOME and JWSDP_HOME these environment variables.
JAXB outlined generation counterparts top of the XML elements and complex types of top-level elements of Java classes and interfaces. In an XML model, the one element from <xs: element />, a complex type elements from <xs: complexType /> said. This tutorial will give a scientific journal said in an article published on the model examples, examples of this model will be bundled JAXB compile. XML schema, catalog.xsd, as follows:
<xsd:schema Xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element Name="catalog" type="catalogType"/>
<xsd:complexType Name="catalogType">
<xsd:sequence>
<Xsd: element ref = "journal"
MinOccurs = "0" maxOccurs = "unbounded" />
</ Xsd: sequence>
<xsd:attribute Name="section" type="xsd:string"/>
<xsd:attribute Name="publisher" type="xsd:string"/>
</ Xsd: complexType>
<xsd:element Name="journal" type="journalType"/>
<xsd:complexType Name="journalType">
<xsd:sequence>
<Xsd: element ref = "article"
MinOccurs = "0" maxOccurs = "unbounded" />
</ Xsd: sequence>
</ Xsd: complexType>
<xsd:element Name="article" type="articleType"/>
<xsd:complexType Name="articleType">
<xsd:sequence>
<xsd:element Name="title" type="xsd:string"/>
<xsd:element Name="author" type="xsd:string"/>
</ Xsd: sequence>
<xsd:attribute Name="level" type="xsd:string"/>
<xsd:attribute Name="date" type="xsd:string"/>
</ Xsd: complexType> </ xsd: schema>
Construction of some of the XML schema is not JAXB support. If these are not included in the support structure of the model, then when you tried to use xjc to generate Java class will be the error. The following models are not supported elements: xs: any, xs: anyAttribute, xs: notation, xs: redefine, xs: key, xs: keyref and xs: unique. Model of the following attributes are not supported: complexType.abstract, element. abstract, element.substitutionGroup, xsi: type, complexType.block, complexType.final, element.block, element.final, schema.blockDefault and schema.finalDefault.
Java class xjc generation tool based on this model to a model to bundle Java class. Examples of this model against bind to the order is:
> Xjc catalog.xsd
Xjc command line interface options out in the table below:
- Nv for input mode does not enforce strict XML validation - b <file> designated external binding document - d <dir> designated generated documents stored path - p <pkg> targeted packages - classpath <arg> designated classpath
- Use packet-runtime <pkg> impl.runtime not be generated - xmlschema input mode is a W3C XML mode (the default)
For example model catalog.xsd, xjc will generate 45 categories, as follows xjc displayed in the output:
Parsing a schema …
Compiling a schema …
Generated \ impl \ runtime \ ErrorHandlerAdaptor.java
Generated \ impl \ runtime \ MSVValidator.java
Generated \ impl \ runtime \ NamespaceContext2.java
Generated \ impl \ runtime \ UnmarshallableObject.java
Generated \ impl \ runtime \ MarshallerImpl.java
Generated \ impl \ runtime \ ValidationContext.java
Generated \ impl \ runtime \ UnmarshallerImpl.java
Generated \ impl \ runtime \ DefaultJAXBContextImpl.java
Generated \ impl \ runtime \ ContentHandlerAdaptor.java
Generated \ impl \ runtime \ GrammarInfoFacade.java
Generated \ impl \ runtime \ UnmarshallingContext.java
Generated \ impl \ runtime \ UnmarshallingEventHandlerAdaptor.java
Generated \ impl \ runtime \ XMLSerializable.java
Generated \ impl \ runtime \ Discarder.java
Generated \ impl \ runtime \ PrefixCallback.java
Generated \ impl \ runtime \ SAXMarshaller.java
Generated \ impl \ runtime \ NamespaceContextImpl.java
Generated \ impl \ runtime \ UnmarshallingEventHandler.java
Generated \ impl \ runtime \ GrammarInfo.java
Generated \ impl \ runtime \ InterningUnmarshallerHandler.java
Generated \ impl \ runtime \ ValidatableObject.java
Generated \ impl \ runtime \ GrammarInfoImpl.java
Generated \ impl \ runtime \ ValidatingUnmarshaller.java
Generated \ impl \ runtime \ ValidatorImpl.java
Generated \ impl \ runtime \ SAXUnmarshallerHandlerImpl.java
Generated \ impl \ runtime \ XMLSerializer.java
Generated \ impl \ runtime \ Util.java
Generated \ impl \ runtime \ SAXUnmarshallerHandler.java
Generated \ impl \ runtime \ AbstractUnmarshallingEventHandlerImpl.java
Generated \ impl \ ArticleImpl.java
Generated \ impl \ ArticleTypeImpl.java
Generated \ impl \ CatalogImpl.java
Generated \ impl \ CatalogTypeImpl.java
Generated \ impl \ JAXBVersion.java
Generated \ impl \ JournalImpl.java
Generated \ impl \ JournalTypeImpl.java
Generated \ Article.java
Generated \ ArticleType.java
Generated \ Catalog.java
Generated \ CatalogType.java
Generated \ Journal.java
Generated \ JournalType.java
Generated \ ObjectFactory.java
Generated \ bgm.ser
Generated \ jaxb.properties
For example XML schema for each top-level xs: element and the top xs: complexType, corresponds to generate a Java interface and a Java class. Also created a factory class (ObjectFactory.java), including the creation of interface object methods. In articles in the sample code files jaxb-java-resources.zip find ObjectFactory.java category.
Catalog.java top of the corresponding elements catalog generation interface. From the model generated by the elements of the interface extends javax.xml.bin.Elemnt category.
Catalog.java:
Package generated; public interface Catalog
Extends javax.xml.bind.Element, generated.CatalogType ()
CatalogType.java top of the corresponding elements catalogType complex formation interface. CatalogType corresponding catalog interface elements for each attribute designated setter and getter methods, and the corresponding catalog elements of the elements of a journal getter methods.
CatalogType.java:
Package generated; (public interface CatalogType
Java.lang.String getSection ();
Void setSection (java.lang.String value);
Java.util.List getJournal ();
Java.lang.String getPublisher ();
Void setPublisher (java.lang.String value);)
CatalogImpl.java and CatalogTypeImpl.java respectively corresponding Catalog.java CatalogType.java interface and the implementation class.
From the Java class to create an XML document in this section, the document will be an example XMl-Jin JAXB from Java class is created. Examples of XML documents, catalog.xml as:
<? Xml version = "1.0" encoding = "UTF-8">
<Catalog xmlns = "http://www.w3.org/2001/XMLSchema-Instance"
Section = "Java Technology"
Publisher = "IBM developerWorks">
<journal>
<article Level="Intermediate" date="January-2004">
<title> Service Oriented Architecture Frameworks </ titl
Tags: java xml






