Skip to main content

accessing element values of substitutionGroups

3 replies [Last post]
icoman
Offline
Joined: 2006-04-18
Points: 0

I'm having trouble retrieving a value from an unmarshalled content tree or updating this value before marshalling out to an XML document. This happens when the element belongs to a substitutionGroup and an @XmlElementRef supplies a List of classes. For example a section of the XML looks like this:

7777
Orginal

The OAGIS schema shows:

I'm assuming that the substitutionGroup causes the xjc to create a DocumentIdType class but not a CustomerDocumentId class.

The getId() and getRevision() methods are in the DocumentIdType class. The getDocumentIdType() method is in the DocumentIds class and returns a List> object. I can't figure out how to get the Id and Revision values after I unmarshall the XML. It marshalls back out OK because I tried that, so there must be someway to get to these values. Section 5.5.5 of the JAXB 2.0 specification and the related references don't supply any examples. Here's what I tried so far:

========================================================
PurchaseOrderHeader poh = npo.getHeader();
DocumentIds dids = poh.getDocumentIds();
List<YYYYY> didt = dids.getDocumentIdType();

for(Iterator iterDIDT = didt.iterator(); iterDIDT.hasNext();)
{
try
{
XXXXX ndidt = (XXXXX)iterDIDT.next();
Revision revision = ndidt.getRevision();
System.out.println("Cust PO#: " + ndidt.getId());
=======================================================

The xjc generated java class, DocumentIds, states that the possible List values are DocumentIdType or PartyDocumentId.

For YYYYY and XXXXX I tried every possible combination of:
1) JAXBElement<? extends DocumentIdType>
2) DocumentIdType
3) PartyDocumentId
4) JAXBElement
5) Object
6) Class

The only value of YYYYY that javac will not bomb with "incompatible types" is 1) or 6), however, I can not use either for XXXXX.

Using 2) or 3) for XXXXX bombs on execution with a ClassCastException.

Only 3) has the getId() and getRevision() methods but I could get a successful execution by leaving these gets out and using either 4) or 5). Interesting on 4) if I used the getName() method to QName I came up with "CustomerDocumentId". Not that this helps any but it shows that this class doesn't have values it has properties.

Anybody know how to get and/or set the Id and the Revision values while in the iteration of the List? Is this supposed to somehow use the get.Superclass() method of the Class class? Would greatly appreciate an example.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kohsuke
Offline
Joined: 2003-06-09
Points: 0

The default code generation is done in such a way that your CustomedDocumentId element is represented as an instance of JAXBElement, where the QName indicates "CustomerDocumentId" and the expected type is "PartyDocumentId".

So your code should look like:
[code]
PurchaseOrderHeader poh = npo.getHeader();
DocumentIds dids = poh.getDocumentIds();
List> didt = dids.getDocumentIdType();

for( JAXBElement ndidt : didt ) {
Revision revision = ndidt.getValue().getRevision();
System.out.println("Cust PO#: " + ndidt.getValue().getId());
[/code]

icoman
Offline
Joined: 2006-04-18
Points: 0

Yep, that worked. Thanks for the example. However, I'm still not sure how the bits and bytes work on this. You changed the ndidt.getId() to ndidt.getValue().getId(). I'm assuming that the getValue() is for the JAXBElement to return the content model and attribute values for this element. Now comes the reverse part.

To marshal this I can setValue(0 and setID() but what kind of object do I .add() to the List? I can createDocumentIdType with the ObjectFactory but I can't add() that to the List in DocumentIds. Would another small code example be too much to ask?

kohsuke
Offline
Joined: 2003-06-09
Points: 0

You'll create a JAXBElement instance to wrap it.

There's a method on ObjectFactory that lets you create JAXBElement conveniently, that takes a DocumentIdType object. Use that method.