Java Reference
In-Depth Information
throws XPathExpressionException {
String name = "";
XPathFactory xpf = XPathFactory.newInstance();
XPath xp = xpf.newXPath();
NodeList resultList = (NodeList)xp.evaluate(
"/employee/name", domResult.getNode(),
XPathConstants.NODESET);
name = resultList.item(0).getTextContent();
System.out.println("Employee Name: " + name);
}
}
The service, which you built in
Marshaling a Custom Type to XML in a Response
,
produces
XML that looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
<id>1</id>
<name>Bill Gates</name>
</employee>
Once you specify the address of the service, you pass it into the
Service.addPort
method
along with the
HTTPBinding
constant instead of the SOAP binding you used earlier. Then use
the request context you get from the
Dispatch
to indicate that you're using the HTTP GET
method. The runtime will use the properties in this map to help it create the proper request and
send it off.
The
Dispatch
returns a
Source
, which can be transformed into a number of
Source
-related
objects suitable for different purposes. Here, you'll examine the result as a
DOMResult
, so pass
a new, empty
DOMResult
object to a
Transformer
along with your
Source
result. The trans-
former will create a DOM view of the returned XML that you can traverse using XPath.
The
printResult
method just applies the XPath expression
/employee/name
to the DOM
tree. You get the text content of the first matching node in the tree, and finally print the result.