November 1, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Using JAXB in JDeveloper 10.1.3

  • January 17, 2008
  • By Deepak Vohra
  • Send Email »
  • More Articles »

If you have used Web Services, you should be familiar that SOAP/XML messages used in data exchange in a Web Service are based on an XML Schema. Or, if you have used J2EE applications, you would be familiar with creating XML deployment descriptors that are based on an XML Schema. But, how is an XML document instantiated from an XML Schema? Java Architecture for XML Binding (JAXB) provides a compiler to bind an XML Schema to its Java representation from which an XML document may be marshalled. Marshalling is the process of creating an XML document from a Java object. JAXB also provides an unmarshalling API to unmarshal an XML document to a Java object. Unmarshalling is the process of converting an XML document to its Java object representation and subsequently reading the element and attribute values in the XML document. Marshalling is used to create an XML document and unmarshalling is used to read an XML document. You will use Oracle JDeveloper for JAXB, because JDeveloper provides a built-in support for JAXB compiler.

Preliminary Setup

JDeveloper 10.1.3 has new feature, a JAX-B compiler. With the JAX-B compiler, an XML Schema may be compiled into its Java representation. You will need to install JDeveloper 10.1.3. In JDeveloper, create an application and a project. In the project, create an XML Schema with File>New>General>XML>XML Schema.



Click here for a larger image.

Figure 1: Creating an XML Schema

In the Create XML Schema window, specify a File Name, and click OK. An XML Schema gets added to the Applications-Navigator.

Figure 2: XML Schema in a JDeveloper Project

You need to add the Oracle XML Parser v2 library, which contains the Java API for XML Parsing (JAXP) and the XML parsers. Also, add the Xml.jar JAR file, which contains the javax.xml.bind package API. A library is added by selecting Tools>Project Properties>Libraries>Add Library.



Click here for a larger image.

Figure 3: Adding JAXB Libraries to JAXB Application

The JAXB compiler generates Java interfaces and classes corresponding to the top-level elements and top-level complexType elements. A schema element is represented with <xs:element/>, and a complexType is represented by <xs:complexType/>. The example schema that will be compiled in this article is listed below.

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:element name="Catalog" type="catalogType"/>
   <xsd:complexType name="catalogType">
      <xsd:sequence>
         <xsd:element name="journal" type="journalType"
                      minOccurs="0" maxOccurs="unbounded"/>
      </xsd:sequence>
      <xsd:attribute name="title" type="xsd:string"/>
      <xsd:attribute name="publisher" type="xsd:string"/>
   </xsd:complexType>

   <xsd:complexType name="journalType">
      <xsd:sequence>
         <xsd:element name="article" type="articleType"
                      minOccurs="0" maxOccurs="unbounded"/>
      </xsd:sequence>
      <xsd:attribute name="date" type="xsd:string"/>
   </xsd:complexType>
   <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="section" type="xsd:string"/>
   </xsd:complexType>
</xsd:schema>

In the next section, you will compile the example schema into Java classes and representation. Subsequently, you will construct and marshal a Java representation to an XML document. You also will unmarshal an XML document using the JAXB API.





Page 1 of 6



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel