background image

Examining DOMSrcExample

<< Examining DOMExample | Running DOMExample and DOMSrcExample >>
<< Examining DOMExample | Running DOMExample and DOMSrcExample >>
164
SOAP
WITH
A
TTACHMENTS
API
FOR
J
AVA
} else {
text = (Text) node;
String content = text.getValue();
System.out.println(indent +
"Content is: " + content);
}
}
}
Examining DOMSrcExample
DOMSrcExample differs from DOMExample in only a few ways. First, after it
parses the document, DOMSrcExample uses the document to create a
DOM-
Source
object. This code is the same as that of DOMExample except for the last
line:
static DOMSource domSource;
...
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File(args[0]));
domSource = new DOMSource(document);
...
Then, after DOMSrcExample creates the message, it does not get the header and
body and add the document to the body, as DOMExample does. Instead, DOM-
SrcExample gets the SOAP part and sets the
DOMSource
object as its content:
// Create a message
SOAPMessage message = messageFactory.createMessage();
// Get the SOAP part and set its content to domSource
SOAPPart soapPart = message.getSOAPPart();
soapPart.setContent(domSource);
The example then uses the
getContents
method to obtain the contents of both
the header (if it exists) and the body of the message.
The most important difference between these two examples is the kind of docu-
ment you can use to create the message. Because DOMExample adds the docu-
ment to the body of the SOAP message, you can use any valid XML file to create
the document. But because DOMSrcExample makes the document the entire
content of the message, the document must already be in the form of a valid
SOAP message, and not just any XML document.