JAXB and Choosing the List Implementation For elements with max occurs greater than one, JAXB will generate a j
Ava. util. ListProperty and the underlying implementation will be
Java. util. ArrayList. You can control which list implementation is used through internal and external schema annotations. you can also use your own domain objects which gives you full control of your object model. this post will discuss these different options.
Option #1-default jaxb generationWe will use the following XML schema for this post:
12345678910111213141516 |
<? xml version = "1.0" encoding = "UTF-8" ?> < xsd:schema xmlns:xsd = "http://www.w3.org/2001/XMLSchema" > < xsd:element name = "customer" > < xsd:complexType > < xsd:sequence > < xsd:element name = "phone-number" type = "xsd:string" maxOccurs = "unbounded" /> </ xsd:sequence > </ xsd:complexType > </ xsd:element > </ xsd:schema > |
Using the following command line:
1 |
xjc -d out customer.xsd |
JAXB will generate the following class. This class hasJava. util. ListPropertyJava. util. arraylistAs the underlying implementation:
123456789101112131415161718192021222324252627 |
package generated; import java.util.ArrayList; import java.util.List; 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; @XmlAccessorType (XmlAccessType.FIELD) @XmlType (name = "" , propOrder = { "phoneNumber" }) @XmlRootElement (name = "customer" ) public class Customer { @XmlElement (name = "phone-number" , required = true ) protected List<String> phoneNumber; public List<STRING> getPhoneNumber() { if (phoneNumber == null ) { phoneNumber = new ArrayList<String>(); } return this .phoneNumber; } } </STRING> |
Option #2-customizing the generationIf you want to control the underlying implementation you can use an external binding file. We will use the bindings file below to change the underlying implementation to beJava. util. Collections list:
123456789101112 |
< 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 = "customer.xsd" > < jxb:bindings node = "//xs:element[@name='customer']/xs:complexType/xs:sequence/xs:element[@name='phone-number']" > < jxb:property collectionType = "java.util.LinkedList" /> </ jxb:bindings > </ jxb:bindings > </ jxb:bindings > |
And the following xjc call:
1 |
xjc -d out -b binding.xml customer.xsd |
To get the following class instead:
123456789101112131415161718192021222324252627 |
package generated; import java.util.LinkedList; import java.util.List; 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; @XmlAccessorType (XmlAccessType.FIELD) @XmlType (name = "" , propOrder = { "phoneNumber" }) @XmlRootElement (name = "customer" ) public class Customer { @XmlElement (name = "phone-number" , required = true ) protected List<STRING> phoneNumber = new LinkedList<STRING>(); public List<STRING> getPhoneNumber() { if (phoneNumber == null ) { phoneNumber = new LinkedList<STRING>(); } return this .phoneNumber; } } </STRING></STRING></STRING></STRING> |
Option #3-using your own domain classGenerally you shoshould interact with the java. util. list interface whenever possible. however in your own domain classes you are free to make your properties Any of the Java. util. list implementations you wish (perhaps to make use of calllikeJava. util. arraylist. trimtosize ()).
12345678910111213141516171819202122232425 |
package com.example; import java.util.ArrayList; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlAccessorType (XmlAccessType.FIELD) @XmlRootElement (name = "customer" ) public class Customer { @XmlElement (name = "phone-number" , required = true ) protected ArrayList<STRING> phoneNumber; public Customer() { phoneNumber = new ArrayList<STRING>(); } public ArrayList<STRING> getPhoneNumber() { return this .phoneNumber; } } </STRING></STRING></STRING> |
From: http://blog.bdoughan.com/2011/01/jaxb-and-choosing-list-implementation.html