Anton Polyakov
Anton Polyakov

Reputation: 331

Unmarshalling single element of XML document with JAXB

I'm very new to JAXB. I have an XML doc which contains serialized object inside one of its element:

<?xml>
 <structure>
   ...blah-blah
   <serializedElement>
     ... JAXB xml block
   </serializedElement>
  </structure>
</xml>

How do I unmarshal such element?

I wrote the following:

org.w3c.dom.Document doc = db.parse(new StringInputStream(rawXml));
org.w3c.dom.Element obj = (org.w3c.dom.Element) doc.getElementsByTagName("serializedElement").item(0);
JAXBElement<MyJaxBObject> je = um.unmarshal(obj, MyJaxBObject.class);
System.out.println(je.getValue());  

but this always return empty value object (although of correct class).

What am I doing wrong?

Thank you!

Upvotes: 4

Views: 5938

Answers (2)

Anton Polyakov
Anton Polyakov

Reputation: 331

Ok, finally. The problem was in missing

dbf.setNamespaceAware(true);

After adding this line everything works fine.

Upvotes: 5

alexblum
alexblum

Reputation: 2238

Try the following:

MyJaxBObject je = javax.xml.bind.JAXB.unmarshal(serializedElementAsString, MyJaxBObject.class);

Upvotes: 4

Related Questions