tags:

views:

36

answers:

1

How can I unmarshal a message that was marshalled using a QName local part different then what is expected?

My example is below:

Given an object to unmarshal that has been created using a marshal method such as

     marshaller.marshal( new JAXBElement(
                        new QName("urn:somenamespace","DifferentNameMessage"), 
                        OriginalMessageType.class, 
                        originalMsg), 
                   result);

(As I understand this code snippet, the QName is indicating to make the root element use a substitue name tag other than what is specified in the original schema. Essentially using a substitute name method. For instance, in the QName above the original tag would have been "NameMessage" but the message was marshalled using local part "DifferentNameMessage".)

I would normally use a method of unmarshalling such as:

     String xmlString = convertStreamToString(in);
     final StringReader xmlReader = new StringReader(xmlString);
     final StreamSource xmlSource = new StreamSource(xmlReader);         

     JAXBContext jaxbContext = JAXBContext.newInstance(OriginalMessageType.class);
     Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
     JAXBElement ret = null;
     ret = unmarshaller.unmarshal(xmlSource, OriginalMessageType.class);

But, if I use this method then the JAXBElement ret name is for the QName that was use to marshalled it, yet the DeclaredType is for OriginalMessageType.class and the unmarshalled message contains null element values for the expected sub-elements, even though the original message contains valid values for these sub-elements.

i am wondering if it is possible to specifiy the substitute QName during unmarshalling to indicate that it is a substitute and then to use the original tag in its place?

A: 

The following is correct:

ret = unmarshaller.unmarshal(xmlSource, OriginalMessageType.class); 

Is this how you made your JAXBContext when your marshalled?

JAXBContext jaxbContext = JAXBContext.newInstance(OriginalMessageType.class);  

Things to try:

  1. Did you verify the marshalled document is correct?
  2. What happens when you use the same JAXBContext for both marshal and unmarshal?
Blaise Doughan
1. I used different JAXBContext for marshal and unmarshal.2. I did not verify, I will do that now.3. I will go back and test marshall/unmarshall with same JAXBContext
That solved the problem. I was using incorrect JAXBContext! Thanks alot.