Adding Content to the SOAPPart Object
Adding Content to the SOAPPart Object
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
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 SOAPHeaderElement 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.
To add a file directly to the SOAPPart object, you use a javax.xml.transform.Source object
from JAXP (the Java API for XML Processing). There are three types of Source objects:
SAXSource
, DOMSource, and StreamSource. A StreamSource object holds an XML document in
text form. SAXSource and DOMSource objects hold content along with the instructions for
transforming the content into an XML document.
The following code fragment uses the JAXP API to build a DOMSource object that is passed to the
SOAPPart.setContent
method. The first three lines of code get a DocumentBuilderFactory
SAAJ Tutorial
Chapter 19 · SOAP with Attachments API for Java
599