Client Code
The full client code is shown in Example 5-7. Fortunately, most of the code is similar to that in the previous three examples. As in the service code, we’ll also utilize the DOM API to build a new XML document.
First, we’ll set the encoding style to XML literal:
call.setEncodingStyleURI(Constants.NS_URI_LITERAL_XML);
This enables the passing of whole XML documents within the SOAP request.
Second, we’ll create a new XML document.
We’ll use the same technique that we used to write
the server code, with the goal of creating a single product element
(e.g., <product sku="A358185"/>
). With the
element in hand, we will then create a new
Parameter
object:
skuParam = new Parameter("productNode", org.w3c.dom.Element.class, productNode, Constants.NS_URI_LITERAL_XML);
Note that we will again set the encoding style to XML literal.
As usual, the final step is to cast the return value correctly. In
this case, we’ll cast to the DOM
Element
class:
Parameter result = resp.getReturnValue ( ); Element xmlResult = (Element) result.getValue( );
We can then print out the Element
string using
Apache’s handy
DOM2Writer
:
DOM2Writer domWriter = new DOM2Writer( ); System.out.println ("Server Response: "); System.out.println (domWriter.nodeToString(product));
package com.ecerami.soap; /** * A Sample SOAP Client * Retrieves Product Info for Specified Stockkeeping Unit (SKU) * Data is returned as an XML Literal Document * usage: java ProductXMLClient sku# */ import ...
Get Web Services Essentials now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.