background image

getChildElements() Method

<< connection.call() Method | Running MyUddiPing >>
<< connection.call() Method | Running MyUddiPing >>
158
SOAP
WITH
A
TTACHMENTS
API
FOR
J
AVA
The method
getChildElements
returns the elements in the form of a
java.util.Iterator
object. You access the child elements by calling the
method
next
on the
Iterator
object. An immediate child of a
SOAPBody
object
is a
SOAPBodyElement
object.
We know that the reply can contain only one
businessList
element, so the code
then retrieves this one element by calling the iterator's
next
method. Note that
the method
Iterator.next
returns an
Object
, which must be cast to the spe-
cific kind of object you are retrieving. Thus, the result of calling
businessListIterator.next
is cast to a
SOAPBodyElement
object:
SOAPBodyElement businessList =
(SOAPBodyElement) businessListIterator.next();
The next element in the hierarchy is a single
businessInfos
element, so the
code retrieves this element in the same way it retrieved the
businessList
. Chil-
dren of
SOAPBodyElement
objects and all child elements from this point forward
are
SOAPElement
objects.
Iterator businessInfosIterator =
businessList.getChildElements(new QName(
"urn:uddi-org:api_v2", "businessInfos"));
SOAPElement businessInfos =
(SOAPElement) businessInfosIterator.next();
The
businessInfos
element contains zero or more
businessInfo
elements. If
the query returned no businesses, the code prints a message saying that none
were found. If the query returned businesses, however, the code extracts the
name and optional description by retrieving the child elements that have those
names. The method
Iterator.hasNext
can be used in a
while
loop because it
returns
true
as long as the next call to the method
next
will return a child ele-
ment. Accordingly, the loop ends when there are no more child elements to
retrieve.
Iterator businessInfoIterator =
businessInfos.getChildElements(
soapFactory.createName("businessInfo",
"", "urn:uddi-org:api_v2"));
if (! businessInfoIterator.hasNext()) {
System.out.println("No businesses found " +
"matching the name \"" + args[1] + "\".");
} else {