background image

Getting a SOAPConnection Object

<< SOAPBody Element | Getting the Content of a Message >>
<< SOAPBody Element | Getting the Content of a Message >>
C
REATING AND
S
ENDING A
S
IMPLE
M
ESSAGE
131
product2.addTextNode("Peach");
childName = soapFactory.new QName("Price");
SOAPElement price2 = order2.addChildElement(childName);
price2.addTextNode("1.48");
The SAAJ code in the preceding example produces the following XML in the
SOAP body:
<PO:PurchaseLineItems
xmlns:PO="http://sonata.fruitsgalore.com">
<Order>
<Product>Apple</Product>
<Price>1.56</Price>
</Order>
<Order>
<Product>Peach</Product>
<Price>1.48</Price>
</Order>
</PO:PurchaseLineItems>
Getting a SOAPConnection Object
The SAAJ API is focused primarily on reading and writing messages. After you
have written a message, you can send it using various mechanisms (such as JMS
or JAXM). The SAAJ API does, however, provide a simple mechanism for
request-response messaging.
To send a message, a SAAJ client can use a
SOAPConnection
object. A
SOAP-
Connection
object is a point-to-point connection, meaning that it goes directly
from the sender to the destination (usually a URL) that the sender specifies.
The first step is to obtain a
SOAPConnectionFactory
object that you can use to
create your connection. The SAAJ API makes this easy by providing the
SOAP-
ConnectionFactory
class with a default implementation. You can get an
instance of this implementation using the following line of code.
SOAPConnectionFactory soapConnectionFactory =
SOAPConnectionFactory.newInstance();
Now you can use
soapConnectionFactory
to create a
SOAPConnection
object.
SOAPConnection connection =
soapConnectionFactory.createConnection();