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

How are you handling the data revolution? We want your take on what's real, what's hype, and what's next in the world of data engineering.

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

  • How to Convert XLS to XLSX in Java
  • Thread-Safety Pitfalls in XML Processing
  • How to Merge Excel XLSX Files in Java
  • How to Query XML Files Using APIs in Java

Trending

  • DevOps Remediation Architecture for Azure CDN From Edgio
  • Zero-Trust AI: Applying Cybersecurity Best Practices to AI Model Development
  • Parallel Data Conflict Resolution in Enterprise Workflows: Pessimistic vs. Optimistic Locking at Scale
  • Containerizing AI: Hands-On Guide to Deploying ML Models With Docker and Kubernetes
  1. DZone
  2. Coding
  3. Languages
  4. XML Schema to Java - Generating XmlAdapters

XML Schema to Java - Generating XmlAdapters

By 
Blaise Doughan user avatar
Blaise Doughan
·
Dec. 30, 11 · Interview
Likes (0)
Comment
Save
Tweet
Share
13.8K Views

Join the DZone community and get the full member experience.

Join For Free

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="http://www.w3.org/2001/XMLSchema">
 
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="number" type="NumberCodeValueType" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
 
    <xs:simpleType name="NumberCodeValueType">
        <xs:restriction base="xs:int">
            <xs:pattern value="[0-7]{7}" />
        </xs:restriction>
    </xs:simpleType>
 
</xs:schema>

NumberFormatter

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);
    }
 
}

bindings.xml

We will leverage a JAXB bindings file to reference the formatter:
<jxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.1">
    <jxb:bindings schemaLocation="format.xsd">
        <jxb:bindings node="//xs:element[@name='number']">
            <jxb:property>
                <jxb:baseType>
                    <jxb:javaType name="java.lang.Integer"
                        parseMethod="blog.xmladapter.bindings.NumberFormatter.parseInt"
                        printMethod="blog.xmladapter.bindings.NumberFormatter.printInt" />
                </jxb:baseType>
            </jxb:property>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>


XJC Call

The bindings file is referenced in the XJC call as:
xjc -d out -p blog.xmladapter.bindings -b bindings.xml format.xsd


Adapter1

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));
    }
 
}


Root

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;
 
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "number"
})
@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;
    }
 
}


Demo

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();
        root.setNumber(4);
 
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }
}


Output

We will get the desired output:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <number>0000004</number>
</root>

 

From http://blog.bdoughan.com/2011/08/xml-schema-to-java-generating.html

XML Schema Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • How to Convert XLS to XLSX in Java
  • Thread-Safety Pitfalls in XML Processing
  • How to Merge Excel XLSX Files in Java
  • How to Query XML Files Using APIs in Java

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: