DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • What Is Ant, Really?
  • Process Mining Key Elements
  • Ensuring Reliable Microservice Deployment With Spring Boot Build Info Maven Plugin
  • How To Generate Scripts of Database Objects in SQL Server

Trending

  • A Deep Dive into Tracing Agentic Workflows (Part 1)
  • A Walk-Through of the DZone Article Editor
  • Why Your AI Agent's Logs Aren't Earning Trust
  • Deployment Lessons You Only Learn the Hard Way
  1. DZone
  2. Coding
  3. Languages
  4. JAXB's @XmlAnyElement(lax=true) Explained

JAXB's @XmlAnyElement(lax=true) Explained

By 
Blaise Doughan user avatar
Blaise Doughan
·
Jan. 02, 13 · Interview
Likes (15)
Comment
Save
Tweet
Share
51.3K Views

Join the DZone community and get the full member experience.

Join For Free

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>



Annotation Element Use case Property (programming) Object (computer science) Document POST (HTTP) Factory (object-oriented programming) Leverage (statistics)

Published at DZone with permission of Blaise Doughan. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • What Is Ant, Really?
  • Process Mining Key Elements
  • Ensuring Reliable Microservice Deployment With Spring Boot Build Info Maven Plugin
  • How To Generate Scripts of Database Objects in SQL Server

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook