background image

Getting a SOAPConnection Object

<< SOAPBody | Sending a Message >>
<< SOAPBody | Sending a Message >>

Getting a SOAPConnection Object

SOAPBody body = message.getSOAPBody();
QName bodyName =
new QName(
"http://sonata.fruitsgalore.com", "PurchaseLineItems", "PO");
SOAPBodyElement purchaseLineItems =
body.addBodyElement(bodyName);
QName childName = new QName(
"Order");
SOAPElement order = purchaseLineItems.addChildElement(childName);
childName = new QName(
"Product");
SOAPElement product = order.addChildElement(childName);
product.addTextNode(
"Apple");
childName = new QName(
"Price");
SOAPElement price = order.addChildElement(childName);
price.addTextNode(
"1.56");
childName = new QName(
"Order");
SOAPElement order2 = purchaseLineItems.addChildElement(childName);
childName = new QName(
"Product");
SOAPElement product2 = order2.addChildElement(childName);
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.
SAAJ Tutorial
The Java EE 5 Tutorial · September 2007
596