Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 478
Anton Polyakov Avatar asked Jan 20 '23 04:01

Anton Polyakov


1 Answers

Ok, finally. The problem was in missing

dbf.setNamespaceAware(true);

After adding this line everything works fine.

like image 71
Anton Polyakov Avatar answered Feb 17 '23 12:02

Anton Polyakov