JAXB and xml binding

JAXB is a tool for processing xml files in Java. The JAXB xjc compiler converts an XML schema file, .xsd, into a set of Java classes that match the hierarchy of the schema. Once these classes are available, an xml file based on the original schema can then be mapped �directly into Java classes where it can be used in your program using the jaxb api.

    This is an appealing concept since it makes processing xml files in Java much easier than the two alternate routes of either

    A. creating custom SAX content handlers or
    B. traversing an in memory DOM tree.

    This is so appealing in fact that I thought I would try it out on the OGC GetCapabilities XML that is produced for a WMS. For example:


    This call to a Geoserver WMS will produce a capabilities xml file. It would be nice to access the layers defined in the xml and use these to determine layer visibility at a given scale.

    The first step was to download the JAXB reference implementation

    I first tried using the early release of jaxb2.0. However, I ran into problems using this version on the OGC getCapabilities xsd schema. The OGC wms schemas found here http://schemas.opengis.net/wms/ were in DTD form for all versions prior to 1.3.0. My first attempt was to use the 1.3.0 xsd and see if it could be modified to work with the 1.1.1 xml output from the Geoserver WMS. I was able to use jaxb2.0 xjc to translate the capabilities_1_3_0.xsd into classes, but I was not able to read the example xml:

    Finally I returned to the older jaxb1.0.6 release version. Again I had no success using this version on the capabilities1_3_0.xsd. Unable to get this working after a bit of effort I went back to the capabilities_1_1_1.dtd found here

    The jaxb 1.0.6 implementation has a dtd transcoder, but it is unstable and didn’t seem to work on the OGC WMS capabilities dtd. So my next stop was trang

    Using trang I was able to transcode the capabilities dtd to xsd :

    java -jar H:\downloads\trang-20030619\trang-20030619\trang.jar
E:\temp\WMS_MS_Capabilities.dtd E:\temp\WMS_MS_Capabilities.xsd

    Once I had the xsd produced from trang I then used the jaxb xjc compiler to create the corresponding classes:

    H:\downloads\JAXB\jaxb-1.0.6\jaxb-ri-1.0.6\bin\xjc -d D:\workspace3.1\jaxb106\src -p com.mmc.wms E:\temp\WMS_MS_Capabilities.xsd

    Unfortunately there was a duplicate HTTP reference in the this .xsd which prevented reading the xml files. I manually altered the .xsd to rename the DCPType type reference to HTTP2:

    <xs:element name=”DCPType” type=”HTTP2″/>
      <!– Available HTTP request methods. One or both may be supported. –>
      <xs:complexType name=”HTTP2″>
            <xs:element ref=”HTTP”/>

    After this small modification of the xsd I reran the xjc resulting in a corrected jaxb class set. I was then able to process a sample WMS capabilities xml. Now I finally had a workable jaxb class set for processing getcapabilities xml.

    Jaxb1.0.6 works but it requires a multitude of jar files along with a complex set of classes and implementation classes. The jaxb2.0 seemed to be much cleaner and I hope that the final release of this version will work better with the OGC 1.3.0 xsd when it becomes available in Geoserver.

    One interesting future development is an extension to the jaxb concept that is being used by the uDig developers called GTXML.

    I have not used this yet but hope to in the future. It appears to add the capability of subsetting the bindings to just the elements needed. It also appears to have a streaming capability that solves the memory issues inherent to large GIS files in GML xml.