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
19 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
Comment Utility
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 run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

To properly understand GitHub, let’s divide it into two words ‘Git’ and ‘Hub’. Git is basically a ‘Distribution Version Control’ (DVC) and ‘Source Code Management’ (SCM) system widely used by software programmers while Hub means the efficient centre…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now