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
44 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unexpected HttpURLConnection connection behavior 2 84
simple java question 3 44
eclipse shortcuts 9 45
split string containing \r\n in Java 46 38
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

911 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

16 Experts available now in Live!

Get 1:1 Help Now