Unmarshalling a single element of an XML document with JAXB

I am very new to JAXB. I have an XML document that contains a serialized object inside one of its elements:

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

How to disable such an 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 returns an empty value object (although the correct class).

What am I doing wrong?

Thanks!

+4
source share
2 answers

Try the following:

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

Good, finally. The problem was the lack of

 dbf.setNamespaceAware(true); 

After adding this line, everything works fine.

+2
source

All Articles