Java Reference
In-Depth Information
soape.addNamespaceDeclaration("xsd",
"http://www.w3.org/
2001/xmlschema");
soape.addNamespaceDeclaration("xsi",
"http://www.w3.org/
2001/xmlschema-instance");
The
SOAPBody
instance contains either content or a fault. Adding content to the
bodyfirstrequiresthatyoucreate
SOAPBodyElement
objects(tostorethiscontent)
andaddtheseobjectstothe
SOAPBody
instance.Thistaskisaccomplishedbycalling
either of
SOAPBody
's two
addBodyElement()
methods, which create the
SOAPBodyElement
object,addittothe
SOAPBody
object,andreturnareferenceto
thecreatedobjectsothatyoucancreatemethodcallchains(see
Chapter2
foradiscus-
sion of chaining together method calls).
When a new subelement of the SOAP
Body
element is created, you must specify
a fully qualified name in the form of a
Name
instance or a
QName
instance. Because
the Java documentation for the
Name
interface states that it may be deprecated in
favor of
QName
, you should get into the habit of using
QName
instead of
Name
.
As a result, you should use
SOAPBody
's
SOAPBodyElement addBodyEle-
ment(QName qname)
methodinsteadofusingthisinterface's
SOAPBodyElement
addBodyElement(Name name)
method, as demonstrated here:
QName name = new QName("http://tutortutor.ca/library",
"gettitle", "lns");
SOAPElement soapel = soapb.addBodyElement(name);
SOAPBodyElement
instancesstoresubelementinstances.Youcreatethesesubele-
mentsandaddthemtothe
SOAPBodyElement
instancebycalling
SOAPElement
's
various
addChildElement()
methods,suchas
SOAPElement addChildEle-
ment(String localName)
, which creates a subelement object having the speci-
fied
localName
, adds this subelement object to the
SOAPBodyElement
object on
whichthismethodiscalled,andreturnsareferencetothecreated
SOAPElement
ob-
ject for chaining together method calls.
You can then attach a text node to a body element or a subelement by calling
SOAPElement
's
SOAPElement addTextNode(String text)
method. You
canalsocall
SOAPElement
's
void setAttribute(String name, String
value)
method(inheritedfrom
SOAPElement
's
org.w3c.dom.Element
ancest-
orinterface)toaddattributestothesubelementasappropriate.Thefollowingexample
demonstrates: