The Beginners Guide to JAXB

The Beginners Guide to JAXB

The Beginners Guide to JAXB

BEGINNING JAXB  ( PART-2)

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

In the second part of this tutorial on JAXB, the author gives a lab-oriented example to demonstrate the practical method of using the JAXB API.Two demos , one for Unmarshalling & the other for creating and marshalling , are given

It is always very important to learn a new concept , by hands-on-experimentation,after getting a bird's eye view.So, we now take up a very simple example and see the step by step procedure.

We are using jwsdp1.4 & jdk1.4.2
-----------------------------------
We have installed jwsdp1.4 in E: DRIVE.
& installed jdk1.4.2 also in E: DRIVE.
Our working folder is: g:\jaxbdemo

In this working folder (g:\jaxbdemo), let us first create a dtd file , named , library.dtd, as follows:
--------------------------------
<?xml version="1.0" encoding="US-ASCII" ?>
<!ELEMENT library (fiction , biography ,science)>
<!ELEMENT fiction (book)+>
<!ELEMENT biography (book)+>
<!ELEMENT science (book)+>
<!ELEMENT book (#PCDATA)>
--------------------------------------------
The following file (g:\jaxbdemo\library.xml)
conforms to the above DTD.
-----------------------------------
<?xml version="1.0" ?>
<!DOCTYPE library SYSTEM "library.dtd">
<library>
<fiction>
<book>IVANHOE</book>
<book>DAVID COPPERFIELD</book>
<book>WAR AND PEACE</book>
</fiction>
<biography>
<book>LIFE OF GANDHI</book>
<book>ABRAHAM LINCOLN</book>
<book>MARX AND ENGELS</book>
</biography>
<science>
<book>MAN THE UNKNOWN</book>
<book>EXPANDING UNIVERSE</book>
<book>ORIGIN OF SPECIES</book>
</science>
</library>
-----------------------------------------
Documentation by SUN & a number of tutorials in the web, ( cited as reference at the end of this tutorial) assume that we are using ANT build file. It is desirable that any tutorial illustrates the procedure without using ANT first, because understanding ANT scripts ,is difficult for beginners. It is easier to understand the script ,after doing it the path/classpath way.This will give us a clearer picture.
--------------------------------------

We begin by setting HOME as follows:
SET JAVA_HOME=E:\JDK1.4.2
SET JWSDP_HOME=E:\JWSDP1.4
--------------------------------------------
In this window, we give path as follows:
( one continuous line).
g:\jaxbdemo\setpath.bat
--------------------
c:\windows\command;
e:\jdk1.4.2\bin;
e:\jwsdp1.4\jaxb\bin;
e:\jwsdp1.4\jwsdp-shared\bin
********************************************
We find the following files in:
e:\jwsdp1.4\jaxb\lib.
jaxb-api.jar
jaxb-impl.jar
jaxb-libs.jar
jaxb-xjc.jar
--------------------------------------
We have to set classpath very carefully.
as , directed by Sun's documentation.
So, we create a batch file for this purpose.
Let us call this batchfile 'setcpath.bat'
g:\jaxbdemo>edit setcpath.bat
>set classpath=g:\jaxbdemo;
e:\jwsdp1.4\jaxb\lib\jaxb-api.jar;
e:\jwsdp1.4\jaxb\lib\jaxb-impl.jar;
e:\jwsdp1.4\jaxb\lib\jaxb-libs.jar;
e:\jwsdp1.4\jaxb\lib\jaxb-xjc.jar;
e:\jwsdp1.4\jswdp-shared\lib\namespace.jar;
e:\jwsdp1.4\jwsdp-shared\lib\jax-qname.jar;
e:\jwsdp1.4\jwsdp-shared\lib\xsdlib.jar
d:\jdk1.4\lib\tools.jar;
e:\jwsdp1.4\jwsdp-shared\lib\relaxngDatatype.jar
e:\jwsdp1.4\jaxp\lib\jaxp.jar
e:\jwsdp1.4\jaxp\lib\endorsed\dom.jar;
e:\jwsdp1.4\jaxp\lib\endorsed\sax.jar;
e:\jwsdp1.4\jaxp\lib\endorsed\xalan.jar;
e:\jwsdp1.4\jaxp\lib\endorsed\xercesImpl.jar;
( this is as per Sun documentation given at the end of this file).
 
(all these typed in a single line).
============================================
We set classpath for our dos window by running the setcpath.bat file.
After this, we run the following command:
g:\jaxbdemo>xjc -dtd library.dtd
 {
we may get a message that dom4j.jar is required and it should be placed in lib folder. dom4j is available in jwsdp1.0\common\lib) So, we copy dom4j.jar from jwsdp1.0\common\lib to jwsdp1.4\jaxb\lib folder).Run setcpath again.
We can now run the command without any problem. } 
we get the following display in the console:
-----------------------------
parsing a schema...
compiling a schema...
generated\impl\runtime\UnmarshallingContext.java
generated\impl\runtime\SAXUnmarshallerHandler.java
generated\impl\runtime\PrefixCallback.java
generated\impl\runtime\Util.java
generated\impl\runtime\GrammarInfoFacade.java
generated\impl\runtime\UnmarshallerImpl.java
generated\impl\runtime\UnmarshallableObject.java
generated\impl\runtime\SAXMarshaller.java
generated\impl\runtime\ValidatableObject.java
generated\impl\runtime\ValidatingUnmarshaller.java
generated\impl\runtime\GrammarInfo.java
generated\impl\runtime\Discarder.java
generated\impl\runtime\SAXUnmarshallerHandlerImpl.java
generated\impl\runtime\InterningUnmarshallerHandler.java
generated\impl\runtime\MarshallerImpl.java
generated\impl\runtime\UnmarshallingEventHandler.java
generated\impl\runtime\ContentHandlerAdaptor.java
generated\impl\runtime\ErrorHandlerAdaptor.java
generated\impl\runtime\UnmarshallingEventHandlerAdaptor.java
generated\impl\runtime\NamespaceContextImpl.java
generated\impl\runtime\GrammarInfoImpl.java
generated\impl\runtime\NamespaceContext2.java
generated\impl\runtime\DefaultJAXBContextImpl.java
generated\impl\runtime\MSVValidator.java
generated\impl\runtime\AbstractUnmarshallingEventHandlerImpl.java
generated\impl\runtime\XMLSerializable.java
generated\impl\runtime\ValidatorImpl.java
generated\impl\runtime\XMLSerializer.java
generated\impl\runtime\ValidationContext.java
generated\impl\BiographyImpl.java
generated\impl\BookImpl.java
generated\impl\FictionImpl.java
generated\impl\JAXBVersion.java
generated\impl\LibraryImpl.java
generated\impl\ScienceImpl.java
generated\Biography.java
generated\Book.java
generated\Fiction.java
generated\Library.java
generated\ObjectFactory.java
generated\Science.java
generated\bgm.ser
generated\jaxb.properties
********************************************
This is as should be. No problem here.
********************************************
We need not give any package name, when giving the above command.
We need not give any -d directive( for output folder), either.
So we get default package name as 'generated'.
We get all the generated files in subfolders of our working directory.
cd to g:\jaxbdemo\generated.
Let us list the files there.
--------------------------------------------
G:\jaxbdemo\generated>dir
 Directory of G:\jaxbdemo\generated
Biography.java
Book.java
Fiction.java
Library.java
ObjectFactory.java
Science.java
bgm.ser
jaxb.properties
10 File(s) 15,323 bytes
=============================================
what files are found in g:\jaxbdemo\generated\Library.java?
--------------------------------
package generated;
public interface Library {
/*
* {@link generated.Fiction}
* {@link generated.Biography}
* {@link generated.Science}
*/
 java.lang.Object getContent();

 void setContent(java.lang.Object value);
 
}
--------------------------------------------
Thus, we understand that the 'generated' folder contains 'interface' files.
--------------------------------------------
what do we find in 'generated\impl' folder?
--------------------------------------------------
G:\jaxbdemo\GENERA~1\impl>dir
 Directory of G:\jaxbdemo\GENERA~1\impl
 
BiographyImpl.java
BookImpl.java
FictionImpl.java
JAXBVersion.java
LibraryImpl.java
ScienceImpl.java

--------------------------------------------------------------
We now cd to 'generated\impl\runtime' folder.
and list the files there.
-------------------------------------------------------
G:\jaxbdemo\GENERA~1\impl\runtime>dir
 Directory of G:\jaxbdemo\GENERA~1\impl\runtime
 
GrammarInfo.java
Discarder.java
SAXUnmarshallerHandlerImpl.java
InterningUnmarshallerHandler.java
MarshallerImpl.java
UnmarshallingEventHandler.java
ContentHandlerAdaptor.java
ErrorHandlerAdaptor.java
UnmarshallingEventHandlerAdaptor.java
NamespaceContextImpl.java
GrammarInfoImpl.java
NamespaceContext2.java
DefaultJAXBContextImpl.java
MSVValidator.java
AbstractUnmarshallingEventHandlerImpl.java
XMLSerializable.java
ValidatorImpl.java
XMLSerializer.java
ValidationContext.java
UnmarshallingContext.java
SAXUnmarshallerHandler.java
PrefixCallback.java
Util.java
GrammarInfoFacade.java
UnmarshallerImpl.java
UnmarshallableObject.java
SAXMarshaller.java
ValidatableObject.java
ValidatingUnmarshaller.java
29 File(s) 177,213 bytes

********************************************
So far, as it should be,as mentioned in the documentation by SUN.!The files in 'runtime' folder are common to all applications. But, 'generated' and 'impl' folders contain a mixture of example-specific as well as general source files.
-------------------------------------------- Now, we should compile all these source files.Note the command very carefully.
g:\jaxbdemo>javac
 generated\*.java generated\impl\*.java
generated\impl\runtime\*.java
( in a continuous line)
If there are any error messages, it can only be due to non-inclusion of any of the jar files mentioned above. Sometimes ( as the author did) , we make spelling mistakes in typing the name of the jar files or in their folder names. Unless we are able to compile these classes successfully, we CANNOT proceed further. It was found that we were able to compile without any problem,if we follow the steps meticulously.
-------------------------------------------
Now, we are ready to write and test a simple demo for utilising these classes.
-----------------------------------------------
Let us create lesson.java as follows:
// g:\jaxbdemo\lesson.java
import java.util.*;
import javax.xml.bind.*;
import java.io.*;
import generated.*;
class lesson
{
public static void main(String args[])
{
try
{
JAXBContext jContext=
JAXBContext.newInstance("generated");
System.out.println("context ok");
 
 Unmarshaller unmarshaller = jContext.createUnmarshaller() ;
 System.out.println("unmarshaller ok");
 
Library   lib = (Library)unmarshaller.unmarshal
(new FileInputStream("library.xml")) ;
 
 System.out.println("library object ready");
 
Fiction fiction = lib.getFiction();
  System.out.println("fiction group is ready");
 
 List list1 = fiction.getBook();
System.out.println("fiction-list is ready!");

int n = list1.size();
System.out.println(""+n);
 
ListIterator k = list1.listIterator(0);
// be careful about the case! ListIterator
 
while(k.hasNext())
{
Object ob = k.next();
Book book1 = (Book)ob;
System.out.println(book1.getContent());
}
}
catch(Exception e1)
{System.out.println(""+e1); }
 
}
}
============================================
We can now compile lesson.java.
g:\jaxbdemo>javac lesson.java
(We are able to compile without hitch).
All that remains to be done is to run the program!We get the following result in the console.
G:\jaxbdemo>java lesson
context ok
unmarshaller ok
library object ready
fiction is ready
fiction-list is ready!
3
IVANHOE
DAVID COPPERFIELD
WAR AND PEACE
============================================
Thus, our effort in using JAXB is a SUCCESS!
=
If we go to e:\jwsdp1.4\jaxb\samples folder, we will find that there are 25 demo
examples, for various situations.Source code for each example also has been given.
 
Readers are encouraged to study the samples and experiment. We can follow exactly same procedure .
---------------------------------------------
********************************************
=========================
This release of JAXB is contained in several JAR files:
JAXB jar files (contained in jwsdp1.4/jaxb/lib)
jaxb-api.jar
The javax.xml.bind interfaces and helper classes
jaxb-xjc.jar
The JAXB RI binding compiler classes
jaxb-impl.jar (formerly named jaxb-ri.jar)
The JAXB RI binding runtime framework classes
jaxb-libs.jar
Supporting classes for the binding compiler and the binding runtime framework
Supporting classes for the binding compiler and the binding runtime framework
JAXP jar files (contained in jwsdp-1.4/jaxp/lib and jwsdp-1.4/jaxp/lib/endorsed)
jaxp-api.jar
The javax.xml.parsers and javax.xml.transform components of JAXP
sax.jar
The APIs and helper classes for the Simple API for XML (SAX)
dom.jar
The APIs and helper classes for the Document Object Model (DOM)
xercesImpl.jar (v2.3.0)
The implementation classes for the SAX and DOM parsers, as well as Xerces-specific implementations of the JAXP APIs
xalan.jar
Xalan is an XSLT processor for transforming XML documents into other document types
Other JAR files
jax-qname.jar (contained in jwsdp-1.4/jwsdp-shared/lib)
A class for handling QNames shared by several JAX* technologies
namespace.jar (contained in jwsdp-1.4/jwsdp-shared/lib)
Classes for handling XML namespace contexts shared by several JAX* technologies
xsdlib.jar (contained in jwsdp-1.4/jwsdp-shared/lib)
XML Schema type library shared by several JAX* technologies (formerly contained in jaxb-libs.jar)
relaxngDatatype.jar (contained in jwsdp-1.4/jwsdp-shared/lib)
RELAX NG datatype library shared by several JAX* technologies (formerly contained in jaxb-libs.jar)
ant.jar (contained in apache-ant/lib)
Ant 1.5.4
DTD
The JAXB RI is shipped with experimental DTD support, which lets you compile XML DTDs.
To compile a DTD test.dtd, run the XJC binding compiler as follows:
$ xjc.sh -dtd test.dtd
All the other command-line options of the XJC binding compiler can be applied, no matter what schema language you use. Similarly, the XJC ant <http://jakarta.apache.org/ant/> task supports DTD. No matter what schema language you use, the generated code will be no different. You'll use the same JAXB API to access the generated code.  
*******************************************

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