September 21, 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 »

UnMarshalling an XML Document

In this section, you will unmarshal an XML document using the JAXB API and the Java classes generated by compiling the example XML schema. A prerequisite for unmarshalling is that the XML document should conform to the XML Schema from which the binding classes are generated. The XML document that you will unmarshal is the same that is marshalled in the previous section. Create a Java application, JAXBUnMarshaller.java, in JDeveloper project JAXB with File>New>General>Java Class. Import the JAXB API. Because JAXBUnMarshaller.java is created in the same package as the binding classes, the binding classes are not required to be imported. If the unmarshalling application is in a different package, import the package containing the binding classes.

Create a JAXBContext object using the static method newInstance(String contextPath). ContextPath is a list of packages that contain the schema derived binding classes.

JAXBContext jaxbContext=JAXBContext.newInstance("jaxb");

Create an Unmarshaller object from the JAXBContext object.

Unmarshaller unmarshaller=jaxbContext.createUnmarshaller();

The Unmarshaller interface provides overloaded unmarshal methods to unmarshal an XML document from a File, InputSource, InputStream, Node Source, or URL object. In the example application, unmarshal from a File object.

CatalogType catalog =
   (CatalogType)unmarshaller.unmarshal(xmlDocument);

Output the 'title' and 'publisher' attributes using getter methods for these attributes.

System.out.println("Title: "+catalog.getTitle());
System.out.println("Publisher: "+catalog.getPublisher());

Obtain a List of JournalType objects, corresponding to 'journal' nodes in the XML document, using the getJournal() method.

java.util.List journalList=catalog.getJournal();
Iterator iterator=journalList.iterator();

Iterate over the List and obtain the JournalType nodes.

while(iterator.hasNext()){
   JournalType journal=(JournalType)iterator.next();
}

Output the 'date' attribute of the 'journal' element using the getDate() method.

System.out.println("Date: "+journal.getDate());

Obtain a List of ArticleType objects for each of JournalType nodes and create an Iterator for the List.

java.util.List articleList=journal.getArticle();
Iterator iter=articleList.iterator();

Iterate over a ArticleType List and output the values of the 'section' attribute and the 'title' and 'author' elements.

while(iter.hasNext()){
   ArticleType article=(ArticleType)iter.next();
   System.out.println("Section: "+article.getSection());
   System.out.println("Title:   "+article.getTitle());
   System.out.println("Author:  "+article.getAuthor());
}

JAXBUnMarshaller.java is listed below.

package jaxb;

import java.io.*;

import java.util.Iterator;
import javax.xml.bind.*;


public class JAXBUnMarshaller
{
   public void unmarshalXMLDocument(File xmlDocument){
      try
      {

         JAXBContext jaxbContext =
            JAXBContext.newInstance("jaxb");

         Unmarshaller unmarshaller =
            jaxbContext.createUnmarshaller();

         CatalogType catalog =
            (CatalogType)unmarshaller.unmarshal(xmlDocument);
         System.out.println("Title:     "+catalog.getTitle());
         System.out.println("Publisher: "+catalog.getPublisher());

         java.util.List journalList=catalog.getJournal();
         Iterator iterator=journalList.iterator();

         while(iterator.hasNext()){
            JournalType journal=(JournalType)iterator.next();
            System.out.println("Date: "+journal.getDate());

            java.util.List articleList=journal.getArticle();
            Iterator iter=articleList.iterator();

            while(iter.hasNext()){
               ArticleType article=(ArticleType)iter.next();
               System.out.println("Section: "+article.getSection());
               System.out.println("Title:   "+article.getTitle());
               System.out.println("Author:  "+article.getAuthor());
            }

         }
      }
      catch (JAXBException e)
      {
         System.out.println(e.toString());
      }
   }
   public static void main (String[] argv)
   {
      JAXBUnMarshaller jaxbUnmarshaller = new JAXBUnMarshaller();
      jaxbUnmarshaller.unmarshalXMLDocument
         (new File("catalog.xml"));
   }
}




Page 5 of 6



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel