views:

132

answers:

2

Hi,

I'm working on a xml schema resolver and I'm using JAXB with XMLSchema.xsd. I experience problems with JAXB, because I don't get classes for all the top level elements. For example for

<xs:element name="maxLength" id="maxLength" type="xs:numFacet"> 

I do not get a class MaxLength or anything like that. Only NumFacet exists.

Anyone else experienced that and could please help me?

Cheers, XLR

A: 

As far as I remember jaxb, the schema compiler xjc creates classes for each complex type of the schema given. Thus, if you like to have a class MaxLength you should add a complex type declaration to your schema:

<xs:complexType name="MaxLength">
<xs:attribute name="value" type="xs:int"/>
</xs:complexType>
<xs:element name="MyMaxLength" type="MaxLength"/>

You should now get a class MaxLength with a member variable value of type integer.

Seriously? I am using the original XMLSchema.xsd, which I do not want to manipulate. In addition as you see there already is an element with a type ;)
XLR
Sure. Doesn't your example say: hey, there is an instance of type numFacet which I call maxLength. In Java, this would be: NumFacet maxLength = new NumFacet(); no need for another class. what should a class MaxLength look like, anyway?
A: 

JAXB will not generate a class for anything that already has a type, and neither do you need one.

If you unmarshal a global element like your maxLength element, then JAXB will return you a JAXBElement wrapping the NumFacet type. Something like this:

JAXBElement<?> root = unmarshaller.unmarshal(myStream);
NumFacet value = (NumFacet) root.getValue();

There are other methods on JAXBElement to find out what the element name was, etc.

xcut