title: Adding default namespace declarations using a generic list wrapper with JAXB
body: <p>I am all new to JAXB and having wrecked my brains for a week over this I would like to ask the following question. How can I obtain default namespace declarations like these using a generic list wrapper as Blaise Doughan indroduced:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;departments xmlns="urn:example.org/departments"&gt;
   &lt;department&gt;
      &lt;iddepartment&gt;1&lt;/iddepartment&gt;
      &lt;department&gt;Deparment A&lt;/department&gt;
      &lt;v_iddepartment&gt;1&lt;/v_iddepartment&gt;
   &lt;/department&gt;
   &lt;department&gt;
      &lt;iddepartment&gt;2&lt;/iddepartment&gt;
      &lt;department&gt;Department B&lt;/department&gt;
      &lt;v_iddepartment&gt;1&lt;/v_iddepartment&gt;
   &lt;/department&gt;
  &lt;/departments&gt;
  
  &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;employees xmlns="urn:example.org/employees"&gt;
   &lt;employee&gt;
      &lt;firstname&gt;Tom&lt;/firstname&gt;
      &lt;lastname&gt;Jones&lt;/lastname&gt;
      &lt;praefix&gt;Dr.&lt;/praefix&gt;
      &lt;birthdate&gt;1970-01-01&lt;/birthdate&gt;
      &lt;ipnumber&gt;1234&lt;/ipnumber&gt;
   &lt;/employee&gt;
   &lt;employee&gt;
      &lt;firstname&gt;Elvis&lt;/firstname&gt;
      &lt;lastname&gt;Knoxville&lt;/lastname&gt;
      &lt;praefix/&gt;
      &lt;birthdate&gt;1970-01-02&lt;/birthdate&gt;
      &lt;ipnumber&gt;4567&lt;/ipnumber&gt;
   &lt;/employee&gt;
  &lt;/employees&gt;
  </code></pre>
  
  <p>I have several annotated classes like these:</p>
  
  <pre><code>package org.bp24.server.table;
  
  import javax.xml.bind.annotation.XmlRootElement;
  import javax.xml.bind.annotation.XmlType;
  
  @XmlRootElement(name = "department", namespace = "urn:example.org/departments")
  @XmlType(propOrder = {"iddepartment", "department", "v_iddepartment"})
  public class Department {
  
  private int iddepartment;
  
  private String department;
  
  private int v_iddepartment;
  
  public int getIddepartment() {
      return iddepartment;
  }
  
  public void setIddepartment(int iddepartment) {
      this.iddepartment = iddepartment;
  }
  
  public String getDepartment() {
      return department;
  }
  
  public void setDepartment(String department) {
      this.department = department;
  }
  
  public int getV_iddepartment() {
      return v_iddepartment;
  }
  
  public void setV_iddepartment(int v_iddepartment) {
      this.v_iddepartment = v_iddepartment;
  }
  
  }
  
  package org.bp24.server.table;
  
  import javax.xml.bind.annotation.XmlRootElement;
  import javax.xml.bind.annotation.XmlTransient;
  import javax.xml.bind.annotation.XmlType;
  
  @XmlRootElement(name = "employee", namespace = "urn:example.org/employees")
  @XmlType(propOrder = {"firstname", "lastname", "praefix", "suffix", "birthdate", "ipnumber"})
  public class Employee {
  
  private int idemployee;
  
  private String firstname;
  
  private String lastname;
  
  private String praefix;
  
  private String suffix;
  
  private String birthdate;
  
  private String ipnumber;
  
  private int v_iduser; 
  
  @XmlTransient
  public int getIdemployee() {
      return idemployee;
  }
  
  public void setIdemployee(int idemployee) {
      this.idemployee = idemployee;
  }
  
  public String getFirstname() {
      return firstname;
  }
  
  public void setFirstname(String firstname) {
      this.firstname = firstname;
  }
  
  public String getLastname() {
      return lastname;
  }
  
  public void setLastname(String lastname) {
      this.lastname = lastname;
  }
  
  public String getPraefix() {
      return praefix;
  }
  
  public void setPraefix(String praefix) {
      this.praefix = praefix;
  }
  
  public String getSuffix() {
      return suffix;
  }
  
  public void setSuffix(String suffix) {
      this.suffix = suffix;
  }
  
  public String getBirthdate() {
      return birthdate;
  }
  
  public void setBirthdate(String birthdate) {
      this.birthdate = birthdate;
  }
  
  public String getIpnumber() {
      return ipnumber;
  }
  
  public void setIpnumber(String ipnumber) {
      this.ipnumber = ipnumber;
  }
  
  @XmlTransient
  public int getV_iduser() {
      return v_iduser;
  }
  
  public void setV_iduser(int v_iduser) {
      this.v_iduser = v_iduser;
  }
  
  }
  </code></pre>
  
  <p>Here is the list wrapper I use:</p>
  
  <pre><code>package org.bp24.server.xml.copy;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import javax.xml.bind.annotation.XmlAnyElement;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlElementWrapper;
  
  public class GenericList &lt;T&gt; {
  
  private List&lt;T&gt; list = new ArrayList&lt;T&gt;();
  
  public GenericList() {
  }
  
  public GenericList(List&lt;T&gt; list) {
      this.list = list;
  }
  
  public void add (T element){
      list.add(element);
  }
  
  public boolean isEmpty(){
      if(list.isEmpty()) return true;
      return false;
  }
  
  public T get (int pos){
      return list.get(pos);
  }
  
  @XmlAnyElement(lax=true)
  public List&lt;T&gt; getList(){
      return list;
  }
  
  }
  </code></pre>
  
  <p>And here is the marshalling code:</p>
  
  <pre><code>package org.bp24.server.xml.copy;
  
  import java.util.HashMap;
  import java.util.List;
  
  import javax.xml.bind.JAXBContext;
  import javax.xml.bind.JAXBElement;
  import javax.xml.bind.JAXBException;
  import javax.xml.bind.Marshaller;
  import javax.xml.namespace.QName;
  
  import org.bp24.server.xml.GenericList;
  
  public class MarshallMatters&lt;T&gt; {
  
  private static volatile HashMap&lt;Class, JAXBContext&gt; contextStore = new HashMap&lt;Class, JAXBContext&gt;();
  
  //synchronized to ensure orderly concurrent contextStore access
  private synchronized JAXBContext getContext (Class clazz) throws JAXBException{
  
      if(contextStore.containsKey(clazz)) return contextStore.get(clazz);
      JAXBContext context = JAXBContext.newInstance(GenericList.class, clazz);
      contextStore.put(clazz, context);
      return context;
  
  }
  
  private Class getClassFromList(List&lt;T&gt; list){
  
      if(!list.isEmpty()) return list.get(0).getClass();
      return null;
  
  }
  
  public void writeXml(List&lt;T&gt; list) throws JAXBException{
  
      Class clazz = getClassFromList(list);
      if(clazz==null){
          System.out.println("Error message");
          return;
      }
      JAXBContext jc = getContext(clazz);
      GenericList&lt;T&gt; genList = new GenericList&lt;T&gt;(list);
      Marshaller m = jc.createMarshaller();
      m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  
      QName qn = new QName(clazz.getSimpleName().toLowerCase() + "s");
      JAXBElement&lt;GenericList&gt; jaxbe = new JAXBElement&lt;GenericList&gt;(qn, GenericList.class, genList);
      m.marshal(jaxbe, System.out);
  
  }
  
  public void readXml(List&lt;T&gt; list) throws JAXBException{
  
      ...
  
  }
  
  }
  </code></pre>
  
  <p>Thank you very much in advance for your help.</p>