title: How to anotate object in java to generate XML with multiple local namespaces
body: <p>I need generate XML from java objects using JAXB in specific format:</p>
  
  <pre><code>&lt;message xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xmlns="http://mynamespace.org/message"&gt;
      &lt;header&gt;
          &lt;name&gt;Message&lt;/name&gt;
          &lt;id&gt;randomUUID&lt;/id&gt;
      &lt;/header&gt;
      &lt;body&gt;
          &lt;messagecontent xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                          xmlns="http://mynamespace.org/messagecontent"&gt;
              &lt;object id="randomUUID" type="form"&gt;
                  &lt;formular xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                            xmlns="http://mynamespace.org/formular"&gt;
                            &lt;name&gt;Username&lt;/name&gt;
                            &lt;address&gt;UserAddress&lt;/address&gt;
                  &lt;/formular&gt;
              &lt;object&gt;
          &lt;/messagecontent&gt;
      &lt;/body&gt;
  &lt;/message&gt;
  </code></pre>
  
  <p>For this type xml document I have 3 object in java:</p>
  
  <p>Message:</p>
  
  <pre><code>@XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "message", propOrder = {
      "header",
      "body"
  })
  @XmlRootElement(name = "message",namespace="http://mynamespace.org/message")
  public class Message {
  
      @XmlElement(name = "header")
      private Message.Header header;
      @XmlElement(name = "body")
      private Message.Body body;
  
      public Header getHeader() {
          return header;
      }
  
      public void setHeader(Header header) {
          this.header= header;
      }
  
      public Body getBody() {
          return body;
      }
  
      public void setBody(Body body) {
          this.body= body;
      }
  
      @XmlAccessorType(XmlAccessType.FIELD)
      @XmlType(name = "header", propOrder = {
          "name",
          "id"
      })
      public static class Header {
  
          @XmlElement(name = "name")
          private String name;
          @XmlElement(name = "id")
          private String id;
  
          public String getName() {
              return name;
          }
  
          public void setName(String name) {
              this.name = name;
          }
  
          public String getId() {
              return id;
          }
  
          public void setId(String id) {
              this.id = id;
          }        
      }
  
      @XmlAccessorType(XmlAccessType.FIELD)
      @XmlType(name = "body", propOrder = {
          "msgContent",
      })
      public static class Body {
  
          @XmlElement(name = "messagecontent")
          private MessageContent msgContent;
  
          public MessageContent getMsgContent() {
              return msgContent;
          }
  
          public void setMsgContent(MessageContent msgContent) {
              this.msgContent = msgContent;
          } 
      }
  }
  </code></pre>
  
  <p>MessageContent:</p>
  
  <pre><code>@XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "messagecontent", propOrder = {
      "object"
  })
  @XmlRootElement(name = "messagecontent",namespace="http://mynamespace.org/messagecontent")
  public class MessageContent{
  
      @XmlElement(name = "object")
      private Object object;
  
      public Object getObject() {
          return object;
      }
  
      public void setObject(Object object) {
          this.object = object;
      }
  
      @XmlAccessorType(XmlAccessType.FIELD)
      @XmlType(name = "object", propOrder = {
          "id",
          "type",
          "formular"
      })
      public static class Object {
  
          @XmlAttribute(name = "id")
          private String id;
          @XmlAttribute(name = "type")
          private String type;
          @XmlElement(name = "formular")
          private Formular formular;
  
          public String getId() {
              return id;
          }
  
          public void setId(String id) {
              this.id = id;
          }
  
          public String getType() {
              return type;
          }
  
          public void setType(String type) {
              this.type = type;
          }
  
          public Formular getFormular() {
              return formular;
          }
  
          public void setFormular(Formular formular) {
              this.formular = formular;
          }
  
  
      }
  }
  </code></pre>
  
  <p>Formular:</p>
  
  <pre><code>@XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "formular", propOrder = {
      "name",
      "address"
  })
  @XmlRootElement(name = "formular",namespace="http://mynamespace.org/formular")
  public class Formular {
  
      @XmlElement(name = "name")
      private String name;
      @XmlElement(name = "address")
      private String address;
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getAddress() {
          return address;
      }
  
      public void setAddress(String address) {
          this.address = address;
      }
  }
  </code></pre>
  
  <p>From this object using this code: </p>
  
  <pre><code>private static void writeToSysOut(Message obj) {
          String result = null;
          try {
              JAXBContext context = JAXBContext.newInstance(Message.class);
              Marshaller m = context.createMarshaller();
              m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
              StringWriter writer = new StringWriter();
              m.marshal(new JAXBElement&lt;Message&gt;(new QName("message"), Message.class, obj), writer);
              result = writer.toString();
          } catch (JAXBException e) {
              System.out.println(e);
          }
          System.out.println(result);
      }
  </code></pre>
  
  <p>I am getting this XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;message xmlns:ns2="http://mynamespace.org/formular" xmlns:ns4="http://mynamespace.org/message" xmlns:ns3="http://mynamespace.org/messagecontent"&gt;
      &lt;header&gt;
          &lt;name&gt;TestXML&lt;/name&gt;
          &lt;id&gt;123456789&lt;/id&gt;
      &lt;/header&gt;
      &lt;body&gt;
          &lt;messagecontent&gt;
              &lt;object id="23456789" type="msgType"&gt;
                  &lt;formular&gt;
                      &lt;name&gt;Some name&lt;/name&gt;
                      &lt;address&gt;Some address&lt;/address&gt;
                  &lt;/formular&gt;
              &lt;/object&gt;
          &lt;/messagecontent&gt;
      &lt;/body&gt;
  &lt;/message&gt;
  </code></pre>
  
  <p>How to change anotation to get xml with namespaces defined in specific attributes like in example XML. I need to have this specific format because other part I work with can't process XML if it is not in this specific format. Thanks</p>