Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Jaxb R The Beginners Guide to JAXB part 3

Related Tutorials


Share on Google+Share on Google+

The Beginners Guide to JAXB part 3

The Beginners Guide to JAXB part 3


(Page-1) (Page-2) (Page-3)

As mentioned earlier, the recommended schema now is XML-SCHEMA. So, it will be useful if we consider a simple example in that category.
Let us now create a folder as g:\jaxbdem
In that folder, copy home.bat,setpath.bat from g:\jaxbdemo of previous lesson.
Then copy setcpath.bat from g:\jaxbdemo.
Change it to begin with g:\jaxbdem; instead of g:\jaxbdemo as our working directory now is g:\jaxbdem.
We will use the example given by Deepak Vohra in a recent web tutorial(with crucial corrections!.)
We edit catalog.xsd as follows:
<xsd:schema xmlns:xsd="">
 <xsd:element name="catalog" type="catalogType"/>
 <xsd:complexType name="catalogType">
<xsd:element ref="journal" minOccurs="0" maxOccurs="unbounded"/>
  <xsd:attribute name="section" type="xsd:string"/>
<xsd:attribute name="publisher" type="xsd:string"/>
 <xsd:element name="journal" type="journalType"/>
 <xsd:complexType name="journalType">
<xsd:element ref="article" minOccurs="0" maxOccurs="unbounded"/>
 <xsd:element name="article" type="articleType"/>
 <xsd:complexType name="articleType">
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="author" type="xsd:string"/>
<xsd:attribute name="level" type="xsd:string"/>
<xsd:attribute name="date" type="xsd:string"/>
Let us now generate the java source files>
>xjc catalog.xsd
It will be noticed that we did not specify
the option -schema . It is the default.
As we have given correct home,path & classpath, the source files are generated as before and we get a list of all the files thus created in the console.
Now also we have 'generated', 'generated\impl' and 'generated\impl\runtime' folders
under g:\jaxbdem folder.
We now compile the source files.
g:\jaxbdem>javac generated\*.java generated\impl\*.java generated\impl\runtime\*.java
We get compilation without any problem atall.
We are now ready to use these classes.
We will create a java object tree and then send it to a file as xml document. The generated xml file will satisfy the schema specified in catalog.xsd.
import java.util.*;
import javax.xml.bind.*;
import generated.*;
class lesson
{ public static void main(String args[])
JAXBContext JContext=JAXBContext.newInstance("generated");
 System.out.println("context ok");
 ObjectFactory factory=new ObjectFactory();
 System.out.println("object factory ready");
Catalog  catalog=(Catalog)(factory.createCatalog());
System.out.println("catalog ready");
 catalog.setSection("Java Technology");
 catalog.setPublisher("IBM developerWorks");
("catalog attributes set ");
Journal journal=(Journal)(factory.createJournal());
java.util.List journalList=catalog.getJournal();
Article article=(Article)(factory.createArticle());
article.setTitle("Service Oriented Architecture Frameworks");
article.setAuthor("Naveen Balani");
java.util.List articleList=journal.getArticle();
System.out.println("tree ready");
  Marshaller marshaller=jContext.createMarshaller();
System.out.println("marshaller ready");
marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
marshaller.marshal(catalog, new FileOutputStream("catalog.xml"));
 System.out.println("java tree converted into xml & filed");
catch(Exception e1)
{System.out.println(""+e1); }
We can now compile
If we execute we get the following messages in the console.
<console message>
context ok
marshaller ok
object factory ready
catalog ready
 catalog attributes set
java tree converted into xml & filed
We can now verify by editing the catalog.xml file.( g:\jaxbdem\catalog.xml)
We find it as shown below.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<catalog publisher="IBM developerWorks" section="Java Technology">
<article date="January-2004" level="Intermediate">
<title>Service Oriented Architecture Frameworks</title>
<author>Naveen Balani</author>
That completes our tutorial.
List of Sample Apps
Many of the sample applications have been given more descriptive names in this release.
samples/bind-choice (formerly part of SampleApp9)
This sample application illustrates how a choice model group is bound to a Java interface by the <jaxb:globalBindings bindingStyle="modelGroupBinding"/> customization.
This example demonstrates how to use the new "-catalog" compiler switch for resolving external entity references.
samples/create-marshal (formerly SampleApp3)
This sample application demonstrates how to use the ObjectFactory class to create a Java content tree from scratch and marshal it to XML data. It also demonstrates how to add content to a JAXB List property.
samples/datatypeconverter (formerly SampleApp7)
This sample application is very similar to the inline-customize sample application (formerly SampleApp6), but illustrates an easier, but not as robust, <jaxb:javaType> customization.
samples/external-customize (formerly SampleApp8)
This sample application is identical to the datatypeconverter sample application (formerly SampleApp7) except that the binding customizations are contained in an external binding file.
samples/fix-collides (formerly part of SampleApp9)
Another binding customization example that illustrates how to resolve name conflicts. Run "ant fail" first to see the compiler output and then look at binding.xjb to see how the errors were resolved. Running "ant" will use the binding customizations to resolve the name conflicts while compiling the schema.
samples/inline-customize (formerly SampleApp6)
This sample application demonstrates how to customize the default binding produced by the XJC binding compiler.
samples/modify-marshal (formerly SampleApp2)
This sample application demonstrates how to modify a java content tree and marshal it back to XML data.
This sample application demonstrates how to use the new JAXB RI Marshaller property "com.sun.xml.bind.namespacePrefixMapper" to customize the namespace prefixes generated during marshalling.
samples/ondemand-validate (formerly SampleApp5)
This sample application demonstrates how to validate a Java content tree at runtime.
In this example, the input document will be unmarshalled a small chunk at a time, instead of unmarshalling the whole document at once.
This sample app demonstrates how a pull-parser can be used with JAXB to increase the flexibility of processing.
This example shows how the use of the jxb:implClass customization and substitution groups can be used to inject virtual functions into the derived Java hierarchy. It compares two equivalent ways of achieving the same processing, but one is much simpler than the other (search for the "enlightened" boolean flag in the code).
This sample application processes a UBL order instance and writes a report to the screen.
samples/unmarshal-read (formerly SampleApp1)
This sample application demonstrates how to unmarshal an instance document into a Java content tree and access data contained within it.
samples/unmarshal-validate (formerly SampleApp4)
This sample application demonstrates how to enable validation during the unmarshal operations.
This example demonstrates how to use one communication channel (such as a socket) to send multiple XML messages, and how that channel can be combined with JAXB.
This example demonstrates how the behavior of the marshalling process can be customized. In this example, an <?xml-stylesheet ... ?> processing instruction is inserted into the marshalled document.
Sample applications that illustrate JAXB RI vendor extensions <vendor.html>.
This example shows how you can use the new JAXB RI Marshaller property "com.sun.xml.bind.characterEscapeHandler" to change the default character escaping behavior.
This sample application illustrate some of the DTD support available in the JAXB RI's extension mode. Please refer to the Vendor Extensions <vendor.html> page for more detail.
This sample application illustrates how W3C XML Schema substitution groups are supported in JAXB RI's extension mode. Please refer to the Vendor Extensions <vendor.html> page for more detail.
This sample shows how to use the new non-standard locator support. By following the instructions in the readme.txt file, you can cause all of the generated impl classes to implement a new interface that provides more information about error locations. When a ValidationEvent happens on your content tree, simply retrieve the object and cast it down to com.sun.xml.bind.extra.Locatable.
This example shows how you can use experimental RELAX NG support.
This sample shows how to use the new non-standard synchronized method support. By following the instructions in the readme.txt, you can cause all of the generated impl class methods signatures to contain the "synchronized" keyword.
This sample app demonstrates type substitution using the W3C XML Schema Part 0: Primer international purchase order schema.
This example demonstrates how to use the <jxb:serializable> and <jxb:superClass> vendor extensions provided by Sun's JAXB RI.

(Page-1) (Page-2) (Page-3)


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 12, 2011

Related Tutorials

Discuss: The Beginners Guide to JAXB part 3  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
October 14, 2011
Update Your Site

Hello, I've come across your terribly designed site many times in my search for programming information but I've never once actually stayed to read the content. The utter lack of necessary code formatting and a more efficient layout make me never want to click into anything related to If you fix the site design, things may change.
March 29, 2012

this jaxb exapmle is very usefull us .......... really thanks to u.