TechnoBuzz

A Techno Blog, mainly about Java

Using JAXB

Marshalling – is the process of traversing a content tree and
writing an XML document that reflects the tree’s content.
Unmarshalling – is the process of reading an XML document and constructing a content tree of Java content objects.

The JAXB binding framework (javax.xml.bind package) is a runtime API that
provides interfaces for unmarshalling, marshalling, and validating XML content
in a Java application.

JAXB is now contained within the Web Services Developer Pack [download] [Tutorial]

C:\jwsdp-1.4\docs\GettingStarted.html

C:\jwsdp-1.4\jaxb

JWSDP_HOME C:\jwsdp-1.4\

Set environment variables for JWSDP_HOME :

C:\>set JWSDP_HOME=C:\jwsdp-1.4\
C:\>echo %JWSDP_HOME%
C:\jwsdp-1.4\
cd C:\jwsdp-1.4\jaxb\samples\ unmarshal-read
This directory contains files po.xsd, po.xml, build.xml, and src/Main.java
po.xsd is the name of the source XML schema
po.xml is the name of the source XML instance file

Use the xjc.bat command to generate JAXB Java classes from the source XML
schema:

C:\jwsdp-1.4\jaxb\samples\unmarshal-read>%JWSDP_HOME%\jaxb\bin\xjc.bat po.xsd -p
primer.po

The -p primer.po switch tells the JAXB compiler to put the generated classes in a Java package named primer.po.

Here is the output:
parsing a schema…
compiling a schema…
primer\po\impl\CommentImpl.java
primer\po\impl\ItemsImpl.java
primer\po\impl\JAXBVersion.java
primer\po\impl\PurchaseOrderImpl.java
primer\po\impl\PurchaseOrderTypeImpl.java
primer\po\impl\USAddressImpl.java
primer\po\impl\runtime\ValidatorImpl.java
primer\po\impl\runtime\SAXUnmarshallerHandlerImpl.java
primer\po\impl\runtime\PrefixCallback.java
primer\po\impl\runtime\GrammarInfoImpl.java
primer\po\impl\runtime\ErrorHandlerAdaptor.java
primer\po\impl\runtime\InterningUnmarshallerHandler.java
primer\po\impl\runtime\XMLSerializable.java
primer\po\impl\runtime\UnmarshallerImpl.java
primer\po\impl\runtime\SAXMarshaller.java
primer\po\impl\runtime\UnmarshallingContext.java
primer\po\impl\runtime\UnmarshallingEventHandlerAdaptor.java
primer\po\impl\runtime\XMLSerializer.java
primer\po\impl\runtime\Util.java
primer\po\impl\runtime\UnmarshallableObject.java
primer\po\impl\runtime\ValidatableObject.java
primer\po\impl\runtime\MarshallerImpl.java
primer\po\impl\runtime\GrammarInfo.java
primer\po\impl\runtime\UnmarshallingEventHandler.java
primer\po\impl\runtime\ValidationContext.java
primer\po\impl\runtime\SAXUnmarshallerHandler.java
primer\po\impl\runtime\AbstractUnmarshallingEventHandlerImpl.java
primer\po\impl\runtime\DefaultJAXBContextImpl.java
primer\po\impl\runtime\Discarder.java
primer\po\impl\runtime\MSVValidator.java
primer\po\impl\runtime\NamespaceContextImpl.java
primer\po\impl\runtime\NamespaceContext2.java
primer\po\impl\runtime\ContentHandlerAdaptor.java
primer\po\impl\runtime\GrammarInfoFacade.java
primer\po\impl\runtime\ValidatingUnmarshaller.java
primer\po\Comment.java
primer\po\Items.java
primer\po\ObjectFactory.java
primer\po\PurchaseOrder.java
primer\po\PurchaseOrderType.java
primer\po\USAddress.java
primer\po\jaxb.properties
primer\po\bgm.ser
C:\jwsdp-1.4\jaxb\samples\unmarshal-read>

C:\jwsdp-1.4\jaxb\samples\unmarshal-read>%JAVA_HOME%\bin\javadoc -package primer.po -sourcepath . -d docs\api -windowtitle “Generated Interfaces for po.xsd”

Update CLASSPATH:
set CLASSPATH=%CLASSPATH%;C:\jwsdp-1.4\jaxb\lib\jaxb-api.jar;C:\jwsdp-1.4\jaxb\lib\jaxb-impl.jar;C:\jwsdp-1.4\jaxb\lib\jaxb-libs.jar;C:\jwsdp-1.4\jaxb\lib\jaxb-xj
c.jar;C:\jwsdp-1.4\jwsdp-shared\lib\activation.jar;C:\jwsdp-1.4\jwsdp-shared\lib\commons-beanutils.jar;C:\jwsdp-1.4\jwsdp-shared\lib\commons-collections.jar;C:\
jwsdp-1.4\jwsdp-shared\lib\commons-digester.jar;C:\jwsdp-1.4\jwsdp-shared\lib\commons-logging.jar;C:\jwsdp-1.4\jwsdp-shared\lib\jaas.jar;C:\jwsdp-1.4\jwsdp-shar
ed\lib\jax-qname.jar;C:\jwsdp-1.4\jwsdp-shared\lib\jta-spec1_0_1.jar;C:\jwsdp-1.4\jwsdp-shared\lib\namespace.jar;C:\jwsdp-1.4\jwsdp-shared\lib\mail.jar;C:\jwsdp
-1.4\jwsdp-shared\lib\relaxngDatatype.jar;C:\jwsdp-1.4\jwsdp-shared\lib\xsdlib.jar;C:\jwsdp-1.4\jaxp\lib\jaxp-api.jar;C:\jwsdp-1.4\jaxp\lib\endorsed\dom.jar;C:\
jwsdp-1.4\jaxp\lib\endorsed\sax.jar;C:\jwsdp-1.4\jaxp\lib\endorsed\xalan.jar;C:\jwsdp-1.4\jaxp\lib\endorsed\xercesImpl.jar

Compile the Main.java

C:\jwsdp-1.4\jaxb\samples\unmarshal-read>%JAVA_HOME%\bin\javac src/Main.java primer\po\*.java primer\po\impl\*.java

I copied the src Main.java and Main.class to the unmarshal-read directory

C:\jwsdp-1.4\jaxb\samples\unmarshal-read>java Main
Ship the following items to:
Alice Smith
123 Maple Street
Cambridge, MA 12345
US

5 copies of “Nosferatu – Special Edition (1929)”
3 copies of “The Mummy (1959)”
3 copies of “Godzilla and Mothra: Battle for Earth/Godzilla vs. King Ghi
dora”

Note that the unmarshal-read comes with a build.xml for use with ant. It performs the two distinct phases for JAXB:

1. Generating and compiling JAXB Java classes from an XML source schema
2. Unmarshalling, validating, processing, and marshalling XML content

Now if all you just want to do is generate the classes for use in an IDE, after ANT does its thing just
grab the folder in the gen-src directory and copy it within the JAVA Source section within a project. You must have the jars listed above in your build path for it to compile. When executing there are two gotchas to be carefull of. You have to determine the base path within the IDE where it looks for input files. I had a problem where it could not find a file. It was either the jaxb property file or the input xml file. Also, the namespace.jar file would not work when executing the Main. It gave me a security error and had to remove the .sfa file from the namespace.jar and rebuild the jar and it worked fine.

Advertisements

July 29, 2004 - Posted by | XML

1 Comment »

  1. Hmm is anyone else experiencing problems with the images on this blog loading?

    I’m trying to find out if its a problem on my end or if it’s the blog.
    Any responses would be greatly appreciated.

    Comment by Power Ecigs Stop Smoking | May 6, 2013 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: