You can add an @XmlSchemaType annotation to your model to control the output
@XmlList
@XmlElement(required = true)
@XmlSchemaType(name="dateTime")
protected List<XMLGregorianCalendar> packorder1;
@XmlList
@XmlElement(required = true)
@XmlSchemaType(name="date")
protected List<XMLGregorianCalendar> packorder2;
Also, XMLGregorianCalendar knows what XML schema type corresponds to the data its holding, see the getXMLSchemaType method on XMLGregorianCalendar:
http://java.sun.com/javase/6/docs/api/javax/xml/datatype/XMLGregorianCalendar.html#getXMLSchemaType()
JAXB will then use that schema type to marshal the document:
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.XmlList;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.datatype.XMLGregorianCalendar;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
public Root() {
packorder1 = new ArrayList<XMLGregorianCalendar>();
packorder2 = new ArrayList<XMLGregorianCalendar>();
}
@XmlList
@XmlElement(required = true)
protected List<XMLGregorianCalendar> packorder1;
@XmlList
@XmlElement(required = true)
protected List<XMLGregorianCalendar> packorder2;
public List<XMLGregorianCalendar> getPackorder1() {
return packorder1;
}
public void setPackorder1(List<XMLGregorianCalendar> packorder1) {
this.packorder1 = packorder1;
}
public List<XMLGregorianCalendar> getPackorder2() {
return packorder2;
}
public void setPackorder2(List<XMLGregorianCalendar> packorder2) {
this.packorder2 = packorder2;
}
}
and
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
Root root = new Root();
DatatypeFactory df = DatatypeFactory.newInstance();
XMLGregorianCalendar dateTime1 = df.newXMLGregorianCalendar("2010-07-27T12:34:56");
root.getPackorder1().add(dateTime1);
XMLGregorianCalendar dateTime2 = df.newXMLGregorianCalendar("2010-03-17T01:02:03");
root.getPackorder1().add(dateTime2);
XMLGregorianCalendar date1 = df.newXMLGregorianCalendar("2010-07-27");
root.getPackorder2().add(date1);
XMLGregorianCalendar date2 = df.newXMLGregorianCalendar("2010-03-17");
root.getPackorder2().add(date2);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
will produce:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<packorder1>2010-07-27T12:34:56 2010-03-17T01:02:03</packorder1>
<packorder2>2010-07-27 2010-03-17</packorder2>
</root>