DUFF
DUFF

Reputation: 454

JAXB bindings for common nested types

I have to unmarshal and process XML from an external system over which I have no control. The XML is in what I believe is the Russian doll format which does not make it easy to process.

The XML share common types which are declared in a common XSD file. But when the JABX generates JAVA classes for these types each outer class contains nested declarations of the common types which makes them different types as far as JAVA is concerned.

I would like to have common JAVA functions to process the common nested types in the XML, but this does not work as in the JAXB classes these common types are unrelated.

This making my JAVA code which needs to process these common types very messy and I've been trying to use JAXB bindings to fix the problem. But I have has no success. So my question is: Given this format of XML/XSD can JAVA code for common types be generated via bindings or some other method?

So an example;

There are two classes in XML CLASSA & CLASSB. Both contain a complex type with one value of type testTYPE which is a string. The XSDs are:

CLASSA.XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:nstype="http://www.test.com/nstypes"
elementFormDefault="qualified">
<xs:import namespace="http://www.test.com/nstypes" schemaLocation="nstypes.xsd"/>
    <xs:element name="CLASSA">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Prop" minOccurs="0">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="PROP" minOccurs="0">
                                <xs:complexType>
                                    <xs:sequence>   
                                        <xs:element name="Value" type="nstype:testTYPE" minOccurs="0"/>
                                    </xs:sequence>  
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>  
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    </xs:schema>

class B has an idential structure, just a different name.

CLASSB.XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:nstype="http://www.test.com/nstypes"
elementFormDefault="qualified">
<xs:import namespace="http://www.test.com/nstypes" schemaLocation="nstypes.xsd"/>
    <xs:element name="CLASSB">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Prop" minOccurs="0">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="PROP" minOccurs="0">
                                <xs:complexType>
                                    <xs:sequence>   
                                        <xs:element name="Value" type="nstype:testTYPE" minOccurs="0"/>
                                    </xs:sequence>  
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>  
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    </xs:schema>

The only element called Value of type nstype:testTYPE is in declared in nstypes.xsd. It's actually a BASETYPE which is a String.

nstypes.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:nstypes="http://www.test.com/nstypes"
targetNamespace="http://www.test.com/nstypes"
elementFormDefault="unqualified">
<xs:complexType name="BASETYPE">
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attribute name="TYPE" fixed="BASETYPE"/>
            <xs:attribute name="derived" use="optional"/>
            <xs:attribute name="readonly" use="optional"/>
            <xs:attribute name="required" use="optional"/>
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>
<xs:complexType name="testTYPE">
    <xs:simpleContent>
        <xs:extension base="nstypes:BASETYPE"/>
    </xs:simpleContent>
</xs:complexType>
</xs:schema>

The XML for a CLASSA and a CLASSB are similar

ClassA.xml

<?xml version="1.0" encoding="UTF-8"?>
<CLASSA>
    <Prop>
        <PROP>
            <Value>AAA</Value>
        </PROP>
    </Prop>     
</CLASSA>

ClassB.xml

<?xml version="1.0" encoding="UTF-8"?>
<CLASSB>
    <Prop>
        <PROP>
            <Value>BBB</Value>
        </PROP>
    </Prop>     
</CLASSB>

Once the JAXB classes have been generated I'd like to write some code like this:

testJAXB

import java.io.InputStream;

import org.generated.CLASSA.CLASSA;
import org.generated.CLASSA.TestTYPE;
import org.generated.CLASSB.CLASSB;

public class testJAXB
{
    // there is a util function     public static Object loadXML(String xmlFile, String fileName) throws Exception;

    public static void main(String[] args) throws Exception
    {
        // create a ClassA & a ClassB
        CLASSA anAClass = (CLASSA) loadXML("C:\\input\\ClassA.xml", "CLASSA");
        CLASSB anBClass = (CLASSB) loadXML("C:\\input\\ClassB.xml", "CLASSB");

        static void printClass(TestTYPE v)
        {
            // as CLASSA.TestTYPE is imported so v is a CLASSA.TestTYPE
            System.out.println(v.toString());
        }

        // this call will work as there is a printClass which takes a CLASSA.TestTYPE
        printClass(anAClass.getProp().getPROP().getValue());

        // this call will not compile  becase there is no printClass which takes a CLASSA.TestTYPE
        printClass(anBClass.getProp().getPROP().getValue());

        System.out.println("complete.");
    }
}   

What I'd actually like to do is have one implementation of the function printClass(). I guess this would have to take an org.generated.TestTYPE and all the JAXB classes would be generated with org.generated.TestTYPEs ratehr than org.generated.CLASSA.TestTYPEs. I'm hoping that can be achieved with some bindings magic. If anyone can point me in the right direction that would be much appreciated.

I'm have C++ rather than JAVA background so apologies if some of my terminology is incorrect.

Jérôme,

That's what I'd like to see but from xjc I see in CLASSA

public CLASSA.Prop getProp()

where CLASSA.Prop is static class which contains a

protected CLASSA.Prop.PROP  

which is another static class which contains a

 protected TestTYPE value;

and TestTYPE is a

org.generated.CLASSA.TestTYPE   

the TestTYPE in CLASSB is a

org.generated.CLASSB.TestTYPE

as the two TestTYPE are nested in different classes they are different types.

essentially, when running xjc I get two files containing the TestTYPE class:

CLASSA/TestTYPE.JAVA

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2012.09.07 at 07:45:23 AM BST 
//

package org.generated.CLASSA;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


/**
* <p>Java class for testTYPE complex type.
* 
* <p>The following schema fragment specifies the expected content contained within this class.
* 
* <pre>
* &lt;complexType name="testTYPE">
*   &lt;simpleContent>
*     &lt;extension base="&lt;http://www.test.com/nstypes>BASETYPE">
*     &lt;/extension>
*   &lt;/simpleContent>
* &lt;/complexType>
* </pre>
* 
* 
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "testTYPE")
public class TestTYPE
    extends BASETYPE
{


}

and 

CLASSB/TestTYPE.JAVA

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2012.09.07 at 07:45:23 AM BST 
//

package org.generated.CLASSB;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


/**
* <p>Java class for testTYPE complex type.
* 
* <p>The following schema fragment specifies the expected content contained within this class.
* 
* <pre>
* &lt;complexType name="testTYPE">
*   &lt;simpleContent>
*     &lt;extension base="&lt;http://www.test.com/nstypes>BASETYPE">
*     &lt;/extension>
*   &lt;/simpleContent>
* &lt;/complexType>
* </pre>
* 
* 
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "testTYPE")
public class TestTYPE
    extends BASETYPE
{


}   

Upvotes: 0

Views: 2427

Answers (2)

YMomb
YMomb

Reputation: 2387

I do not think that JAXB will factorise such a code. IMO the easier solution is to write the code of the class Prop and define in the binding file that both B.Prop and A.Prop are relying on the same class.

For instance using the classImpl attribute. http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/2.0/tutorial/doc/JAXBUsing4.html#wp148576

Upvotes: 1

J&#233;r&#244;me Radix
J&#233;r&#244;me Radix

Reputation: 10533

Given this format of XML/XSD can JAVA code for common types be generated via bindings or some other method?

Use the xjc tool of the JDK.

Put all your files in a directory, then do :

xjc *.xsd

You'll see that it generates files :

parsing a schema...
compiling a schema...
generated\CLASSA.java
generated\CLASSB.java
generated\ObjectFactory.java
com\test\nstypes\BASETYPE.java
com\test\nstypes\ObjectFactory.java
com\test\nstypes\TestTYPE.java
com\test\nstypes\package-info.java

You'll see that in CLASSA.java you have a method call getProp() that returns you a CLASSA.Prop on which you can do another getProp() :

In CLASSA you have:

public CLASSA.Prop getProp()

In CLASSA.Prop you have :

public CLASSA.Prop.PROP getPROP()

In CLASSA.Prop.PROP the value is of type com.test.nstypes.TestTYPE which is your common type not dependent on CLASSA or CLASSB.

Upvotes: 0

Related Questions