background image

Getting the Content of a Message

<< Getting a SOAPConnection Object | Adding Content to the Header >>
<< Getting a SOAPConnection Object | Adding Content to the Header >>
132
SOAP
WITH
A
TTACHMENTS
API
FOR
J
AVA
You will use
connection
to send the message that you created.
Sending a Message
A SAAJ client calls the
SOAPConnection
method
call
on a
SOAPConnection
object to send a message. The
call
method takes two arguments: the message
being sent and the destination to which the message should go. This message is
going to the stock quote service indicated by the
URL
object
endpoint
.
java.net.URL endpoint = new URL(
"http://wombat.ztrade.com/quotes");
SOAPMessage response = connection.call(message, endpoint);
The content of the message you sent is the stock symbol SUNW; the
SOAPMes-
sage
object
response
should contain the last stock price for Sun Microsystems,
which you will retrieve in the next section.
A connection uses a fair amount of resources, so it is a good idea to close a con-
nection as soon as you are finished using it.
connection.close();
Getting the Content of a Message
The initial steps for retrieving a message's content are the same as those for giv-
ing content to a message: Either you use the
Message
object to get the
SOAPBody
object, or you access the
SOAPBody
object through the
SOAPPart
and
SOAPEnve-
lope
objects.
Then you access the
SOAPBody
object's
SOAPBodyElement
object, because that is
the element to which content was added in the example. (In a later section you
will see how to add content directly to the
SOAPPart
object, in which case you
would not need to access the
SOAPBodyElement
object to add content or to
retrieve it.)
To get the content, which was added with the method
SOAPElement.addText-
Node
, you call the method
Node.getValue
. Note that
getValue
returns the value
of the immediate child of the element that calls the method. Therefore, in the fol-
lowing code fragment, the
getValue
method is called on
bodyElement
, the ele-
ment on which the
addTextNode
method was called.