November 25, 2014
Hot Topics:

Using JAXB in JDeveloper 10.1.3

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

Compiling an XML Schema

In this section, you will compile the example XML schema using the JAX-B compiler in JDeveloper 10.1.3. To compile the example XML schema, select the schema node in Applications-Navigator and select Tools>JAXB Compilation.



Click here for a larger image.

Figure 4: Compiling an XML Schema with the JAXB Compiler

In the JAXB Compilation window, specify a package name to which the schema is to be compiled and click OK.

The schema gets compiled and the Java classes get generated in the specified package. An interface and an implementation class get generated for each of the top level element and complexType definitions. Interface Catalog.java and implementation class CatalogImpl.java get generated, corresponding to top level element 'catalog'. Catalog.java is listed below.

package jaxb;
public interface Catalog extends jaxb.CatalogType,
   javax.xml.bind.Element
{
}

The Catalog.java interface extends the javax.xml.bind.Element interface. The CatalogImpl.java class is listed below.

package jaxb;

public class CatalogImpl extends jaxb.CatalogTypeImpl
   implements jaxb.Catalog
{
   public CatalogImpl(oracle.xml.parser.v2.XMLDocument ownerDoc)
   {
      super("Catalog", "", ownerDoc);
   }

   public CatalogImpl(java.lang.String name,
                      java.lang.String namespace,
                      oracle.xml.parser.v2.XMLDocument ownerDoc)
   {
      super(name, namespace, ownerDoc);
   }

   public CatalogImpl(oracle.xml.parser.v2.XMLElement node)
   {
      super(node);
   }

}

The implementation class provides three constructors. One of the constructors takes a oracle.xml.parser.v2.XMLDocument class object as an argument. Another constructor takes an oracle.xml.parser.v2.XMLElement node as an argument. A third constructor takes a String element tag name, a String element namespace, and an XMLDocument object as arguments.

An interface, CatalogType.java, also gets generated for top level complexType catalogType. The CatalogType.java interface consists of getter and setter methods for the title and publisher attributes. CatalogType.java also includes a getter method for the journal node. The getJournal() method returns a List. CatalogType.java is listed below.

package jaxb;
public interface CatalogType
   public void setTitle(java.lang.String value);
   public java.lang.String getTitle();
   public void setPublisher(java.lang.String value);
   public java.lang.String getPublisher();
   public java.util.List getJournal();
}

Similarly,the JournalType.java and ArticleType.java interfaces get generated corresponding to journalType and articleType complexTypes. Corresponding to these interfaces, implementation classes CatalogTypeImpl.java, JournalTypeImpl.java, and ArticleTypeImpl.java get generated.

An ObjectFactory.java factory class also gets generated. The factory class consists of create methods to create instances of interfaces corresponding to complexType and element definitions. ObjectFactory.java class also consists of an unmarshal (Node) method to unmarshal an XML document node to an object of type Catalog. ObjectFactory.java class is listed below.

package jaxb;
public class ObjectFactory
{
   public jaxb.Catalog createCatalog()
   {
      jaxb.Catalog elem = new jaxb.CatalogImpl(ownerDocument);
      return elem;
   }
   public jaxb.JournalType createJournalType()
   {
      jaxb.JournalType elem =
         new jaxb.JournalTypeImpl(ownerDocument);
      return elem;
   }
   public jaxb.CatalogType createCatalogType()
   {
      jaxb.CatalogType elem =
         new jaxb.CatalogTypeImpl(ownerDocument);
      return elem;
   }

   public jaxb.ArticleType createArticleType()
   {
      jaxb.ArticleType elem =
         new jaxb.ArticleTypeImpl(ownerDocument);
      return elem;
   }
   public Object newInstance (Class javaContentInterface)
      throws javax.xml.bind.JAXBException
   {
      Object newInstance = null;
      java.lang.String elemName = javaContentInterface.getName();
      try
      {
         if (elemName.equals("jaxb.Catalog"))
         {
            newInstance = new jaxb.CatalogImpl(ownerDocument);
            return newInstance;
         }
      }
      catch (Exception e)
      {
         throw new javax.xml.bind.JAXBException(e.toString());
      }
      return null;
   }
   public Object getProperty(String name)
   {
      return null;
   }
   public void setProperty(String name, Object value)
   {
   }
   public Object unmarshal(org.w3c.dom.Node node)
      throws javax.xml.bind.UnmarshalException
   {
      java.lang.String elemName = node.getLocalName();
      try
      {
         if (elemName.equals("Catalog"))
         {
            jaxb.Catalog unode =
               new jaxb.CatalogImpl((oracle.xml.parser.v2.
                                     XMLElement)node);
            return unode;
         }
      }
      catch (Exception e)
      {
         throw new javax.xml.bind.
            UnmarshalException(e.toString());
      }
      return null;
   }
   public Object getSchemaLocation() throws Exception
   {
      return new java.io.File("null").toURL();
   }
   private oracle.xml.parser.v2.XMLDocument ownerDocument =
      new oracle.xml.parser.v2.XMLDocument();
}




Page 2 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