Team lead for the TopLink/EclipseLink JAXB & SDO implementations, and the Oracle representative on those specifications. Blaise is a DZone MVB and is not an employee of DZone and has posted 44 posts at DZone. You can read more from them at their website. View Full User Profile

JAXB's @XmlAnyElement(lax=true) Explained

01.02.2013
| 10972 views |
  • submit to reddit

In a previous post I introduced how the @XmlAnyElement(lax=true) annotation could be used to create a very flexible mapping.  In this post I'll dig a bit deeper into how it relates to both the @XmlRootElement and @XmlElementDecl.

@XmlAnyElement (Root)

The any field is annotated with @XmlAnyElement(lax=true). This means that for that field if an element is associated with a class via @XmlRootElement or @XmlElementDecl then an instance of the corresponding class will be used to populate the field if not the element will be set as an instance of org.w3c.dom.Element

package blog.anyelement;
 
import java.util.List;
import javax.xml.bind.annotation.*;
 
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
 
    @XmlAnyElement(lax = true)
    protected List<Object> any;
 
}

@XmlRootElement (Foo)

Below is an example of a class annotated with @XmlRootElement.

package blog.anyelement;
 
import javax.xml.bind.annotation.*;
 
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
 
    private String fooProp;
 
}

@XmlElementDecl (Bar and ObjectFactory)

Below is an example of a class without the @XmlRootElement annotation. In this use case we will leverage the @XmlElementDecl annotation on a factory class (usually called ObjectFactory) annotated with @XmlRegistry.

package blog.anyelement;
 
import javax.xml.bind.annotation.*;
 
@XmlAccessorType(XmlAccessType.FIELD)
public class Bar {
 
    private String barProp;
 
}

Below is an example of specifying an @XmlElementDecl annotation for the Bar class.

package blog.anyelement;
 
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;
 
@XmlRegistry
public class ObjectFactory {
 
    @XmlElementDecl(name="bar")
    public JAXBElement<Bar> createBar(Bar bar) {
        return new JAXBElement<Bar>(new QName("bar"), Bar.class, bar);
    }
 
}

Input (input.xml)

Below is the input document we'll use for this example. There are 3 elements that correspond to the any property. The first corresponds to the @XmlRootElement annotation on the Foo class. The second corresponds to the @XmlElementDecl annotation for the Bar class and the third does not correspond to any of the domain classes.

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <foo>
        <fooProp>Hello</fooProp>
    </foo>
    <bar>
        <barProp>World</barProp>
    </bar>
    <other>
        <a>A</a>
        <b>B</b>
    </other>
</root>

Demo

In the demo code below we will unmarshal the input document, then output the classes of the objects in the resulting any property and then marshal the payload object back to XML.

package blog.anyelement;
 
import java.io.File;
import javax.xml.bind.*;
 
public class Demo {
 
    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class, Foo.class, ObjectFactory.class);
         
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/blog/anyelement/input.xml");
        Root payload = (Root) unmarshaller.unmarshal(xml);
         
        for(Object o : payload.any) {
            System.out.println(o.getClass());
        }
         
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(payload, System.out);
    }
 
}


Output

Below is the output from running the demo code. Note the classes corresponding to the objects in the any property. The foo element became an instance of the Foo class. The bar element became an instance of JAXBElement that holds an instance of Bar. The other element became an instance of org.w3c.dom.Element.

class blog.anyelement.Foo
class javax.xml.bind.JAXBElement
class com.sun.org.apache.xerces.internal.dom.ElementNSImpl
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <foo>
        <fooProp>Hello</fooProp>
    </foo>
    <bar>
        <barProp>World</barProp>
    </bar>
    <other>
        <a>A</a>
        <b>B</b>
    </other>
</root>



Published at DZone with permission of Blaise Doughan, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)