background image

SOAPHeader

<< Adding Content to the Body | SOAPBody >>
<< Adding Content to the Body | SOAPBody >>

SOAPHeader

You might recall that the headers and content in a SOAPPart object must be in XML format. The
SAAJ API takes care of this for you, building the appropriate XML constructs automatically
when you call methods such as addBodyElement, addChildElement, and addTextNode. Note
that you can call the method addTextNode only on an element such as bodyElement or any child
elements that are added to it. You cannot call addTextNode on a SOAPHeader or SOAPBody object
because they contain elements and not text.
The content that you have just added to your SOAPBody object will look like the following when
it is sent over the wire:
<SOAP-ENV:Envelope
xmlns:SOAP-ENV=
"http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<m:GetLastTradePrice xmlns:m=
"http://wombat.ztrade.com">
<symbol>SUNW</symbol>
</m:GetLastTradePrice>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Let's examine this XML excerpt line by line to see how it relates to your SAAJ code. Note that an
XML parser does not care about indentations, but they are generally used to indicate element
levels and thereby make it easier for a human reader to understand.
Here is the SAAJ code:
SOAPMessage message = messageFactory.createMessage();
SOAPHeader header = message.getSOAPHeader();
SOAPBody body = message.getSOAPBody();
Here is the XML it produces:
<SOAP-ENV:Envelope
xmlns:SOAP-ENV=
"http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
...
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
The outermost element in this XML example is the SOAP envelope element, indicated by
SOAP-ENV:Envelope
. Note that Envelope is the name of the element, and SOAP-ENV is the
namespace prefix. The interface SOAPEnvelope represents a SOAP envelope.
The first line signals the beginning of the SOAP envelope element, and the last line signals the
end of it; everything in between is part of the SOAP envelope. The second line is an example of
an attribute for the SOAP envelope element. Because a SOAP envelope element always contains
SAAJ Tutorial
The Java EE 5 Tutorial · September 2007
594