Java tutorial
import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.Marshaller; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlType; import javax.xml.namespace.QName; public class Main { public static void main(String[] args) throws Exception { MyMessage sarm = new MyMessage(); MyGroupResponse[] sgr = new MyGroupResponse[2]; sgr[0] = new MyGroupResponse(); sgr[1] = new MyGroupResponse(); sarm.setSailingGroup(sgr); JAXBElement<MyMessage> rootElement = new JAXBElement<MyMessage>(new QName("root"), MyMessage.class, sarm); JAXBContext jc = JAXBContext.newInstance(MyMessage.class, MyGroup.class, MyGroupResponse.class); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(rootElement, System.out); } @XmlType(propOrder = { "type", "sailingGroup" }) public static class MyMessage { private MyGroupResponse[] sailingGroup; String type; public MyGroupResponse[] getSailingGroup() { return sailingGroup; } public void setSailingGroup(MyGroupResponse[] sailingGroup) { this.sailingGroup = sailingGroup; } @XmlAttribute public String getType() { return type; } public void setType(String type) { this.type = type; } } public static class MyGroupResponse extends MyGroup { } public static class MyGroup { } }