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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • MLOps: Definition, Importance, and Implementation
  • SRE vs. DevOps
  • Microservices Decoded: Unraveling the Benefits, Challenges, and Best Practices for APIs
  • How To Integrate Microsoft Team With Cypress Cloud

Trending

  • MLOps: Definition, Importance, and Implementation
  • SRE vs. DevOps
  • Microservices Decoded: Unraveling the Benefits, Challenges, and Best Practices for APIs
  • How To Integrate Microsoft Team With Cypress Cloud
  1. DZone
  2. Coding
  3. Languages
  4. Using XmlAdapter for Testing

Using XmlAdapter for Testing

Blaise Doughan user avatar by
Blaise Doughan
·
May. 02, 12 · Interview
Like (0)
Save
Tweet
Share
5.36K Views

Join the DZone community and get the full member experience.

Join For Free
The XmlAdapter mechanism in JAXB (JSR-222) allows an unmappable class to be converted to a mappable one.  An XmlAdapter can be registered with the @XmlJavaTypeAdapter at the field, property, type, or package level.  This post will focus on the scope of the XmlAdapter when registered at the package level.


Package 1 - xmladapter.bar

package-info

When an XmlAdapter is registered at the package level it must include the type parameter.  The type parameter specifies the Java class that this XmlAdapter will be applied to.  For this package (xmladapter.bar) we will specify an XmlAdapter (StringAdapter) that will be applied to all fields/properties of type String within this package.

@XmlJavaTypeAdapters({
    @XmlJavaTypeAdapter(value=StringAdapter.class, type=String.class)
})
package xmladapter.bar;

import javax.xml.bind.annotation.adapters.*; 


StringAdapter

Our XmlAdapter will simply convert all instances of String to upper case when marshalling:

package xmladapter.bar;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class StringAdapter extends XmlAdapter<String, String> {

    @Override
    public String unmarshal(String v) throws Exception {
        return v;
    }

    @Override
    public String marshal(String v) throws Exception {
        if(null == v) {
            return v;
        }
        return v.toUpperCase();
    }

}


Bar

Bar represents a POJO in this package with a property (name) of type String:

package xmladapter.bar;

public class Bar {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}


Package 2 - xmladapter.foo

Foo

Foo represents a domain object with a property of type String that exists in a different package (xmladapter.foo). The XmlAdapter we registered for the xmladapter.bar package will not apply to String fields/properties on this class:

package xmladapter.foo;

import javax.xml.bind.annotation.*;
import xmladapter.bar.Bar;

@XmlRootElement
@XmlType(propOrder={"name", "bar"})
public class Foo {

    private String name;
    private Bar bar;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Bar getBar() {
        return bar;
    }

    public void setBar(Bar bar) {
        this.bar = bar;
    }

}


Demo

The following code will create instances of both Foo and Bar and marshal them to XML.  Note how the values set on the name properties for both Foo and Bar are lower case:

package xmladapter;

import javax.xml.bind.*;
import xmladapter.bar.Bar;
import xmladapter.foo.Foo;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Foo.class, Bar.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        Foo foo = new Foo();
        foo.setName("foo");

        Bar bar = new Bar();
        bar.setName("bar");
        foo.setBar(bar);
 
        marshaller.marshal(foo, System.out);
    }

}


Output

Notice how the value of the name element within bar has been converted to upper case (line 5), but he name element within foo has not (line 3):

<?xml version="1.0" encoding="UTF-8"?>
<foo>
    <name>foo</name>
    <bar>
        <name>BAR</name>
    </bar>
</foo>


Further Reading

If you enjoyed this post then you may also be interested in:

  • JAXB and Joda-Time: Dates and Times
digg_url = 'http://blog.bdoughan.com/2012/02/jaxb-and-package-level-xmladapters.html';

From http://blog.bdoughan.com/2012/02/jaxb-and-package-level-xmladapters.html

Strings Data Types Property (programming) POST (HTTP) Element Java (programming language) Marshalling (computer science) XML Convert (command)

Opinions expressed by DZone contributors are their own.

Trending

  • MLOps: Definition, Importance, and Implementation
  • SRE vs. DevOps
  • Microservices Decoded: Unraveling the Benefits, Challenges, and Best Practices for APIs
  • How To Integrate Microsoft Team With Cypress Cloud

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: