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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

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

  • Lessons Learned in Test-Driven Development
  • The Battle of the Frameworks: Choosing the Right Tech Stack
  • Event Storming Workshops: A Closer Look at Different Approaches
  • One Checkbox to Cloud: Migrating from Tosca DEX Agents to E2G
  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 (12)
Comment
Save
Tweet
Share
50.6K 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, DZone MVB. 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.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: