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
97 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 36

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
The task of choosing a web design company to build a website for your business should never be taken in a light manner. Provided the fact that your website will act as a representative to your business and will be responsible for imposing an online …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

690 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