Solved

How to simplify a groovy script that reads a WS because the result being sent back is simpler

Posted on 2016-11-10
1
62 Views
Last Modified: 2016-11-12
I want to take the output of a web service from webserviceX and assign it to a local variable in an application.

Currently this is done by using groovy script to read the output of the Weather Service WS.  The script is shown below:-

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

// Clean response xml document
responseDocumentBody.normalizeDocument();
// Get result node
NodeList resultList = responseDocumentBody.getElementsByTagName("GetWeatherResult");
Element resultElement = (Element) resultList.item(0);
String weatherDataAsXML = resultElement.getTextContent();

// Check for empty result
if ("Data Not Found".equalsIgnoreCase(weatherDataAsXML))
    return null;

// Parse embedded XML of result
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource inputSource = new InputSource();
inputSource.setCharacterStream(new StringReader(weatherDataAsXML));
Document weatherDataDocument = documentBuilder.parse(inputSource);
Node weatherNode = weatherDataDocument.getDocumentElement();

// Save weather data
Map<String,String> data = new HashMap<String,String>();
NodeList childNodes = weatherNode.getChildNodes();
for (int i=0; i<childNodes.getLength(); i++)
{
        Node node = childNodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE)
        {
                String key = node.getNodeName();
                String value = node.getTextContent();
                data.put(key, value);
        }
}
return data;

Open in new window


The weather service ws returns a bunch of data in a string which is then parsed out into an array.

I would like to try this with a different web service from the WebserviceX site, specifically the Length/Distance Convertor WS (located here).

However, the script needs to change so that it isn't reading a big string which it then parses but just a double

HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <ChangeLengthUnitResponse xmlns="http://www.webserviceX.NET/">
      <ChangeLengthUnitResult>[b]double[/b]</ChangeLengthUnitResult>
    </ChangeLengthUnitResponse>
  </soap12:Body>
</soap12:Envelope>

Open in new window


so what changes need to be made to the script so that it just reads the double that is sent back from the web service and not a load of key pairs that are sent back from the get weather WS.

TIA
0
Comment
Question by:Anthony Horner
1 Comment
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 41883915
It should be as simple as this...

// Clean response xml document
responseDocumentBody.normalizeDocument();
// Get result node
NodeList resultList = responseDocumentBody.getElementsByTagName("ChangeLengthUnitResult");
Element resultElement = (Element) resultList.item(0);
Double value = Double.valueOf(resultElement.getTextContent());

return value;

Open in new window

0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For some reason Google Apps has decided to reserve 'www' for your new domain which results in an error when trying to map the web address 'www' to a certain site in your Google Sites. The error is 'this address is already being used' and after some …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question