November 23, 2014
Hot Topics:

Using JAXB in JDeveloper 10.1.3

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

The interfaces, implementation classes, and ObjectFactory.java class generated from compiling an XML schema are shown in the Applications-Navigator.

Figure 5: Java Classes Generated with the JAXB Compiler

Marshalling an XML Document

Marshalling is the construction of an XML document from a Java representation of the document. In this section, you will marshal an XML document. The XML document that will be marshaled is listed below.

<?xml version="1.0" encoding = 'UTF-8'?>
<catalog title="Oracle Magazine"
         publisher="Oracle Publishing">
   <journal date="July-August 2005">
      <article section="TECHNOLOGY">
         <title>Tuning Undo Tablespace</title>
         <author>Kimberly Floss</author>
      </article>
   </journal>

   <journal date="July-August 2005">
      <article section="TECHNOLOGY">
         <title>Using PHP 5 with Oracle XML DB</title>
         <author>Yuli Vasiliev</author>
      </article>
   </journal>
</catalog>

You need to create a Java application to marshal an XML document. Select File>New>General>Java Class to add a Java class. In the Create Java class frame, specify class name (JAXBMarshaller) and package name (jaxb) and click OK. Java class JAXBMarshaller.java gets added to package jaxb. In the marshaller application, import the JAXB API package.

import javax.xml.bind.*;

Create an instance of JaxbContextImpl.

JaxbContextImpl jaxbContext=new JaxbContextImpl();

Create a Marshaller object from the JaxbContextImpl object. The Marshaller interface is used to marshal a Java object to an XML document.

Marshaller marshaller=jaxbContext.createMarshaller();

Create an ObjectFactory object, which is used to create instances of Java representations of XML Schema elements and complexTypes. Create an object of type Catalog using createCatalog() method.

ObjectFactory factory=new ObjectFactory();
Catalog catalog=factory.createCatalog();

Set 'title' and 'publisher' attributes using setter methods for these attributes.

catalog.setTitle("Oracle Magazine");
catalog.setPublisher("Oracle Publishing");

Create an object of type JournalType, which corresponds to journalType complexType in example XML schema. Set 'date' attribute of JournalType object.

JournalType journal=factory.createJournalType();
journal.setDate("July-August 2005");

Create an ArticleType object and set the 'section' attribute and 'title' and 'author' elements.

ArticleType article=factory.createArticleType();
   article.setSection("Technology");
   article.setTitle("Tuning Undo Tablespace");
   article.setAuthor("Kimberly Floss");

Obtain a ArticleType List from the JournalType object and add the ArticleType object created to the JournalType object.

java.util.List articleList=journal.getArticle();
articleList.add(article);

Obtain a JournalType List from Catalog object and add the JournalType object created to the Catalog object.

java.util.List journalList=catalog.getJournal();
journalList.add(journal);

Similarly, add another JournalType object. The Marshaller interface provides overloaded marshal() methods to marshal a Java object to either a ContentHandler object, a Node, an OutputStream, a Result object, or a Writer object. In the example application, the Java object is marshaled to an XML document using an OutputStream.

marshaller.marshal(catalog, new FileOutputStream(xmlDocument));




Page 3 of 6



Comment and Contribute

 


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

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel