August 25, 2011

XML Schema to Java - Generating XmlAdapters

In previous posts I have demonstrated how powerful JAXB's XmlAdapter can be when starting from domain objects.  In this example I will demonstrate how to leverage an XmlAdapter when generating an object model from an XML schema.  This post was inspired by an answer I gave to a question on Stack Overflow (feel free to up vote).

XMLSchema (format.xsd)

The following is the XML schema that will be used for this example.  The interesting portion is a type called NumberCodeValueType.  This type has a specified pattern requiring it be a seven digit number.  This number can have leading zeros which would not be marshalled by JAXB's default conversion of numbers.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="">

    <xs:element name="root">
                <xs:element name="number" type="NumberCodeValueType" />

    <xs:simpleType name="NumberCodeValueType">
        <xs:restriction base="xs:int">
            <xs:pattern value="[0-7]{7}" />



Since JAXB's default number to String algorithm will not match our schema requirements, we will need to write our own formatter.  We are required to provide two static methods one that coverts our type to the desired XML format, and another that converts from the XML format.

package blog.xmladapter.bindings;

public class NumberFormatter {

    public static String printInt(Integer value) {
        String result = String.valueOf(value);
        for(int x=0, length = 7 - result.length(); x<length; x++) {
            result = "0" + result;
        return result;

    public static Integer parseInt(String value) {
        return Integer.valueOf(value);



We will leverage a JAXB bindings file to reference the formatter:

<jxb:bindings xmlns:xs=""
    xmlns:jxb="" version="2.1">
    <jxb:bindings schemaLocation="format.xsd">
        <jxb:bindings node="//xs:element[@name='number']">
                    <jxb:javaType name="java.lang.Integer"
                        printMethod="blog.xmladapter.bindings.NumberFormatter.printInt" />

XJC Call

The bindings file is referenced in the XJC call as:

xjc -d out -p blog.xmladapter.bindings -b bindings.xml format.xsd


This will cause an XmlAdapter to be created that leverages the formatter:

package blog.xmladapter.bindings;

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

public class Adapter1 extends XmlAdapter<String, Integer> {

    public Integer unmarshal(String value) {
        return (blog.xmladapter.bindings.NumberFormatter.parseInt(value));

    public String marshal(Integer value) {
        return (blog.xmladapter.bindings.NumberFormatter.printInt(value));



The XmlAdapter will be referenced from the domain object using the @XmlJavaTypeAdapter annotation:

package blog.xmladapter.bindings;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlType(name = "", propOrder = {
@XmlRootElement(name = "root")
public class Root {

    @XmlElement(required = true, type = String.class)
    @XmlJavaTypeAdapter(Adapter1 .class)
    protected Integer number;

    public Integer getNumber() {
        return number;

    public void setNumber(Integer value) {
        this.number = value;



Now if we run the following demo code:

package blog.xmladapter.bindings;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Root root = new Root();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);


We will get the desired output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

Further Reading

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


  1. Hi Blaise, question, why is it necessary to add a "name" attribute to the jxb:javaType element in the bindings.xml file? What's it for? Thanks!

  2. Hi Glen,

    The "name" attribute is a required attribute on the jxb:javaType element. The schema to Java compiler will throw an exception if it is not present.

    I don't have a good answer as to why the schema to Java compiler requires it, as it could by default derive the Java type from the XML schema type.


  3. Thanks for sharing. Learning how to override parseMethod and printMethod was very useful for one of my project.


Note: Only a member of this blog may post a comment.