Home > Articles > Web Services > XML

  • Print
  • + Share This
Like this article? We recommend

Servlet Land

The final piece in this dynamic picture is to actually trigger the transformation when a call comes in. To do this, instead of mapping the incoming call to a hand-coded Voice XML document, we link the call to a Java servlet (or other server technologies capable of transform execution) that does the transformation for us and returns the output—our Voice XML.

While we’re not going to get into the details of deploying Java servlets, the important point to recognize here is that the servlet code to dynamically generate our Voice XML is just a few lines of Java that sets things up to access the XML input file, transform it, and send the Voice XML output back to the client.

   // Get the XML input document and the stylesheet.
   Source xmlSource = 
    new StreamSource(new URL("file", "", xmlFileName).openStream());
   Source xsltSource = 
    new StreamSource(new URL("file", "", xsltFileName).openStream());

   // Create a transformer object based on our xslt code
   Transformer transformer = tFactory.newTransformer(xsltSource);

   // Perform the transformation, sending the Voice XML
   //  out the output stream, to the voice platform.
   transformer.transform(xmlSource, new StreamResult(out));

The reason the server code is so simple, is that all the work is being done by the XSLT transform. Yes, you could write a parser to extract the data from the XML data file and then write code to assemble the Voice XML. But once you get a little XSLT under your belt, you’ll be amazed at how much you can do with so little code. For the complete servlet code, visit the web site at http://www.frankcoyle.org.

  • + Share This
  • 🔖 Save To Your Account