background image

SOAPBody Element

<< SOAP envelope element | Getting a SOAPConnection Object >>
<< SOAP envelope element | Getting a SOAPConnection Object >>
130
SOAP
WITH
A
TTACHMENTS
API
FOR
J
AVA
Here is the SAAJ code:
QName name = new QName("symbol");
SOAPElement symbol = bodyElement.addChildElement(name);
symbol.addTextNode("SUNW");
Here is the XML it produces:
<symbol>SUNW</symbol>
The
String "SUNW"
is the text node for the element
<symbol>
. This
String
object is the message content that your recipient, the stock quote service,
receives.
The following example shows how to add multiple
SOAPElement
objects and add
text to each of them. The code first creates the
SOAPBodyElement
object
purchaseLineItems
, which has a fully qualified name associated with it. That
is, the
QName
object for it has a namespace URI, a local name, and a namespace
prefix. As you saw earlier, a
SOAPBodyElement
object is required to have a fully
qualified name, but child elements added to it, such as
SOAPElement
objects, can
have
Name
objects with only the local name.
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);