XML Schema to Java - Generating XmlAdapters
Join the DZone community and get the full member experience.
Join For FreeIn 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).
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>
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); } }
<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 -d out -p blog.xmladapter.bindings -b bindings.xml format.xsd
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)); } }
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; } }
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); } }
<?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
Opinions expressed by DZone contributors are their own.
Comments