3 November 2010

JAXB annotations and how to use them as you wish

I need to use JAXB to create an XML output for a class. This output is generated when JAXB is being called through JETTY framework (i.e. the most probably Jersey is calling JAXB within the Jetty framework).
I have found that JAXB by itself is not creating the XML schemas. However, they are needed to be consumed by JAXB. XML schemas are essential for JAXB to function (i.e. I haven’t seen a tutorial that is based only on a Java Class; every tutorial is discussing how an XML schema is being created, after parsing a specific Class.)
I also have found that within JAXB, there is a part (package or class) called schemagen that is responsible for creating the XML schemas. I think this class should be used to generate the default XML schema and then use JAXB.
Note 1: It sounds like I need to create the Schema and then use the marshalling on the Class. The only thing remaining is the values of the elements in XML; I don’t want to marshal the class but the object of it.
Note 2 : The class is needed to be mapped to the XML schema so that the object can be marshallized.
Schemagen can be located at C:\Program Files\Java\jdk1.6.0_21\bin. It is an .exe file. Thus, it would not be easy to be used within a java program.
In here it has been said that:
The jaxb-xjc.jar file contains the SchemaGenTask.class file, which allows the schema generator to be invoked from the Ant build tool.
It seems that you need to have your packages annotated in order for JAXB to be able to create schema on that. This is writing from Alex Miler that describes this annotation. This and this is the annotation specification for packages in Java Documents. In these definitions you can not find various package annotation elements to be used, it simply defines what a package annotation is.
Annotations may be used on package declarations, with the restriction that at most one annotated package declaration is permitted for a given package.
One other possible way to use the JAXB is to have a file called jaxb.index in your class path. Not sure yet what that file is and how is working.
Problem solution found:
You need to have a class passed to the newInstance(), you can simply get the class you need to marshalize using the .getClass function.
JAXBContext jContext = JAXBContext.newInstance (this.getClass ());
References:
  1. http://download.oracle.com/docs/cd/E17802_01/webservices/webservices/docs/2.0/jaxb/samples.html
  2. http://download.oracle.com/javase/6/docs/api/javax/xml/bind/JAXBContext.html
  3. http://download.oracle.com/javase/6/docs/api/javax/xml/bind/JAXBContext.html#newInstance(java.lang.Class...)
  4. http://java.sun.com/docs/books/jls/third_edition/html/packages.html#7.4.1.1
  5. http://tech.puredanger.com/2007/02/28/package-annotations/
  6. http://blogs.sun.com/darcy/entry/javadoc_tip_prefer_package_info
  7. http://download.oracle.com/javase/6/docs/technotes/tools/solaris/javadoc.html#sourcefiles
  8. http://cmaki.blogspot.com/2007/09/annotated-jaxb-classes.html
  9. http://www.oracle.com/technetwork/articles/javase/index-140168.html#binsch
  10. http://download.oracle.com/javaee/5/tutorial/doc/bnazf.html
  11. http://download.oracle.com/javase/6/docs/technotes/tools/share/schemagen.html
  12. https://jaxb.dev.java.net/guide/
  13. https://jaxb.dev.java.net/tutorial/
  14. http://fisheye5.cenqua.com/browse/jaxb/dist/samples/samples-src/j2s-create-marshal
  15. http://publib.boulder.ibm.com/infocenter/wasinfo/v6r1/index.jsp?topic=/com.ibm.websphere.wsfep.multiplatform.doc/info/ae/ae/twbs_jaxbjava2schema.html

2 comments:

  1. Hello fellow Canadian,

    I came across your article on Twitter. JAXB implementations do not require an XML schema. You can simply start with Java classes and annotate them as necessary to control the XML output. You can check out my blog for examples (http://bdoughan.blogspot.com).

    -Blaise

    ReplyDelete
  2. Hey Blaise,
    Thanks for your comment,
    as you thoroughly mentioned in your blog, I finally figured out how to handle the JAXB annotations.
    Cheers,
    OneCitizen

    ReplyDelete