views:

142

answers:

2

In one of my projects I use JAXB2 marshaller, having a contract-first web service I generate the objects from a XML Schema.

Everything works just fine. But, I have a "code usability" issue. Let me give you an example.

Schema:

<xs:complexType name="personContractAlertListType">
    <xs:sequence>
        <xs:element ref="PersonContractAlert" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
</xs:complexType>


<xs:element name="PersonContractAlertsResponse">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="PersonContractAlertList"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

So in order to access the list of PersonContractAlerts I have to call:

PersonContractAlertsResponse.getPersonContractAlertListType().getPersonContractAlert()

Which is kinda long.

My question is: How can I get rid of the getPersonContractAlertListType() and go directly to: PersonContractAlertsResponse.getPersonContractAlert()

Because that wrapper element is really only for XSD, I don't need it in my Java object.

In other words have:

<Element1>
<Wrapper>
<Element2/>
</Wrapper>
</Element1>

And I want it in Java to map to: Element1.getElement2()

Maybe using JAXB adaptors. And remember I don't want to touch generated objects. This has to be done either in the marshaller settings (adaptar, interceptor, etc) or in the XSD (maybe there are some settings to manipulate).

Thanks a lot!

UPDATE:

I found a tutorial on some binding operations:

https://jaxb.dev.java.net/guide/Using_different_datatypes.html

I will research try using JAXB bindings.

A: 

To get the object model you want, you could use the XPath based mapping extension in EclipseLink JAXB (MOXy).

Blaise Doughan
But the question was if I can do that without changing the generated class.
Alexandru Luchian
To go between the XML and object you want you'll need an XmlAdapter and an customization to make use of it. Changing your model may be cleaner.
Blaise Doughan
A: 

I'm not sure about your particular situation, but in general, typing an extra 30 characters is probably not a big enough cost to justify even searching for a solution to this. Especially since Eclipse will auto-complete that method call.

Gabriel
You missed the point of the question
Alexandru Luchian