• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 135
  • Last Modified:

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

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
Anthony Horner
Asked:
Anthony Horner
1 Solution
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now