title: How to properly format a SOAP message envelope using a custom SOAPHandler
body: <p>I have a class that implements the <a href="http://java.sun.com/javase/6/docs/api/javax/xml/ws/handler/soap/SOAPHandler.html" rel="noreferrer">SOAPHandler interface</a>.  The handleMessage is defined as:</p>
  
  <pre><code>public boolean handleMessage(SOAPMessageContext context) {
  
    SOAPMessage msg = context.getMessage();
    SOAPPart part = msg.getSOAPPart();
    SOAPEnvelope envelope = part.getEnvelope();
  
    // add namespaces
    SOAPElement envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
    envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-      
  
    // add the header with additional elements
    Name qname = envelope.createName("Security", "sse", "http://example.com/security.xsd");
    element = envelope.addHeader().addChildElement(qname);
  
    qname = envelope.createName("mustUnderstand");
    element.addAttribute(qname, "1");
  
    qname = envelope.createName("UsernameToken", "sse", "http://example.com/user.xsd");
    element = envelope.getHeader().addHeaderElement(qname);
    element.addTextNode("user1");
  
    qname = envelope.createName("Password");
    element = envelope.getHeader().addHeaderElement(qname);
    element.addTextNode("1234");
  
  }
  
  } catch (Exception e) {
    e.printStackTrace();
  }
    return true;
  }
  </code></pre>
  
  <p>This generates the following message:</p>
  
  <pre><code>&lt;S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
    &lt;S:Header&gt;
      &lt;sse:Security xmlns:sse="http://example.com/security.xsd" mustUnderstand="1"/&gt;
      &lt;sse:UsernameToken xmlns:sse="http://example.com/user.xsd"&gt;user1&lt;/sse:UsernameToken&gt;
    &lt;/S:Header&gt;
    &lt;S:Body&gt;
      ....The rest of the transaction
    &lt;/S:Body&gt;
  &lt;/S:Envelope&gt;
  </code></pre>
  
  <p>The problem is I need to generate a message with the following format:</p>
  
  <pre><code>&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
     &lt;soapenv:Header&gt;
        &lt;sse:Security soapenv:mustUnderstand="1" xmlns:sse="http://example.com/security.xsd"&gt;
           &lt;sse:UsernameToken wsu:Id="UsernameToken-9993341" xmlns:wsu="http://example.com/user.xsd"&gt;
              &lt;sse:Username&gt;user1&lt;/sse:Username&gt;
              &lt;sse:Password Type="http://example.com/password#PasswordText"&gt;1234&lt;/sse:Password&gt;
           &lt;/sse:UsernameToken&gt;
        &lt;/sse:Security&gt;
     &lt;/soapenv:Header&gt;
    &lt;soapenv:Body&gt;
      ....The rest of the transaction
    &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>The "mustUnderstand" attribute doesn't have the soapenv prefix, the sse:Security tag is closed right away instead of having the other tags as children, and the UserName isn't properly formatted as </p>
  
  <pre><code>&lt;sse:Username&gt;user1&lt;/sse:Username&gt;
  </code></pre>
  
  <p>.  How can I format the message properly using the <a href="http://java.sun.com/webservices/docs/1.5/api/javax/xml/soap/SOAPElement.html" rel="noreferrer">SOAPElement</a> methods?  The biggest thing I need to know is how to properly next the tags inside of the Security tag and how to have the username/password tags properly formatted.</p>
  
  <p>I've tried different combinations of the addHeaderElement and addChildElement methods, but I can't get it formatted properly and the javadocs don't give enough detail about what they will generate.</p>