Jaxb: how to unmarshall xs:any XML-string part?

@XmlAnyElement(lax = true) means in plain English something like:

Dear JAXB! If you have a mapping for this element, please unmarshal it
into a Java object. If you don’t know this element, just leave it as a
DOM element.

This is exactly what is happening in your case. So if you want to actually unmarshal the content of this lax any, provide JAXB context with a mapping for the element you wish to unmarshal. The easiest way to do this is to annotate your class with @XmlRootElement

@XmlRootElement(name="foo", namespace="urn:bar")
public class MyClass { ... }

Now when you create your JAXB context, add MyClass into it:

JAXBContext context = JAXBContext.newInstance(A.class, B.class, ..., MyClass.class);

In this case, if JAXB meets the {urn:bar}foo element in the place of that xs:any, it will know that this element is mapped onto MyClass and will try to unmarshal MyClass.

If you are creating JAXB context based on the package name (you probably do), you can still add you class (say, com.acme.foo.MyClass) to it. The easiest way is to create a com/acme/foo/jaxb.index resource:

com.acme.foo.MyClass

And the add your package name to the context path:

JAXBContext context = JAXBContext.newInstance("org.dar.gee.schema:com.acme.foo");

There are other ways with ObjectFactory etc., but the trick with jaxb.index is probably the easiest one.

Alternatively, instead of unmarshalling everything in one run, you can leave the content of xs:any as DOM and unmarshal it into the target object in a second unmarshalling with anothe JAXB context (which know your MyClass class). Something like:

JAXBContext payloadContext = JAXBContext.newInstance(MyClass.class);
payloadContext.createUnmarshaller().unmarshal((Node) myPayload.getAny());

This approach is sometimes better, especially when you have a combination of container/payload schemas which are relatively independent. Depends on the case.

All said above applies to marshalling as well. It’s all neatly bidirectional.

Leave a Comment