Service Code
The full service code is shown in
Example 5-6. Note that the
ProductXMLService class extends
ProductService from the previous example and
therefore utilizes the same product hashtable. Note also that the new
getProduct( ) method accepts a DOM
Element and returns a DOM
Element. Within the method, we’ll
first extract the SKU attribute code:
String sku = request.getAttribute("sku");We will then search the product hashtable. If a match is found, we
can build an entire XML document via the DOM API. As a shortcut, we
can use the Apache utility method to retrieve a
DocumentBuilder object:
DocumentBuilder docBuilder = XMLParserUtils.getXMLDocBuilder( ); Document doc = docBuilder.newDocument( );
With the document in hand, we can then proceed to add the proper hierarchy of XML elements. For example, the following code creates a product name element with the corresponding text subelement:
Text productNameText = doc.createTextNode(product.getName( ));
Element nameNode = doc.createElement("name");
nameNode.appendChild(productNameText);package com.ecerami.soap; import java.util.Hashtable; import org.w3c.dom.*;import javax.xml.parsers.DocumentBuilder;import org.apache.soap.util.xml.XMLParserUtils;/** * A Sample SOAP Service * Provides Product Name for requested Stockkeeping Unit (SKU) * Information is passed as Literal XML Documents. */ public class ProductXMLService extends ProductService{ /** * Provides Product Info for requested XML document. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access