background image

Adding Content to the SOAPPart Object

<< Adding Content to the Header | DocumentBuilderFactory >>
<< Adding Content to the Header | DocumentBuilderFactory >>
134
SOAP
WITH
A
TTACHMENTS
API
FOR
J
AVA
new
SOAPHeaderElement
object to it. This
SOAPHeaderElement
object contains
the correct qualified name and attribute for a WS-I conformance claim header.
SOAPHeader header = message.getSOAPHeader();
QName headerName = new QName(
"http://ws-i.org/schemas/conformanceClaim/",
"Claim", "wsi");
SOAPHeaderElement headerElement =
header.addHeaderElement(headerName);
headerElement.addAttribute(new QName("conformsTo"),
"http://ws-i.org/profiles/basic/1.1/");
At this point,
header
contains the
SOAPHeaderElement
object
headerElement
identified by the
QName
object
headerName
. Note that the
addHeaderElement
method both creates
headerElement
and adds it to
header
.
A conformance claim header has no content. This code produces the following
XML header:
<SOAP-ENV:Header>
<wsi:Claim
xmlns:wsi="http://ws-i.org/schemas/conformanceClaim/"
conformsTo="http://ws-i.org/profiles/basic/1.1/"/>
</SOAP-ENV:Header>
For more information about creating SOAP messages that conform to WS-I, see
the Conformance Claim Attachment Mechanisms document described in the
Conformance section of the WS-I Basic Profile.
For a different kind of header, you might want to add content to
headerElement
.
The following line of code uses the method
addTextNode
to do this.
headerElement.addTextNode("order");
Now you have the
SOAPHeader
object
header
that contains a
SOAPHeaderEle-
ment
object whose content is
"order"
.
Adding Content to the SOAPPart Object
If the content you want to send is in a file, SAAJ provides an easy way to add it
directly to the
SOAPPart
object. This means that you do not access the
SOAPBody
object and build the XML content yourself, as you did in the preceding section.