SOAP message parsing in Tizen Web
PUBLISHED
Introduction
In this tip document, a simple example of parsing SOAP structure messages using XmlHttpRequest is discussed. SOAP stands for Simple Object Access Protocol. When publishing complex application program interface (API) as a web service, SOAP is very useful as it is commonly used. A Tizen web app is developed to show a step by step approach for implementing SOAP structured message parsing.
Test Settings:
Type |
Tizen Web App |
SDK |
Tizen SDK 2.3.1 |
Tested on |
Wearable Emulator (Circle) |
SOAP Message Structure
A SOAP message is an ordinary XML document containing the following elements −
- Envelope − Defines the start and the end of the message. It is a mandatory element.
- Header − Contains any optional attributes of the message used in processing the message, either at an intermediary point or at the ultimate end-point. It is an optional element.
- Body − Contains the XML data comprising the message being sent. It is a mandatory element.
- Fault − An optional Fault element that provides information about errors that occur while processing the message
Figure 1 : SOAP Message Structure
What is XmlHttpRequest?
XmlHttpRequest can be used to send HTTP requests to and receive responses from a web server. The data returned from XmlHttpRequest calls will often be provided by back-end databases. Besides XML, XmlHttpRequest can be used to fetch data in other formats, e.g. JSON or even plain text. It supports Cross-origin Request Sharing (CORS) which helps to consume SOAP Service from JavaScript.
Using Simple XML HTTP Requests
The example below shows how to create an XML HTTP request on the client side, using JavaScript code that requests Ajax communication:
<script> var method = "GET"; var url = "http://example.com"; var xhr = new XMLHttpRequest(); xhr.open(method, url, true); xhr.send(); </script>
XmlHttpRequest have many other methods along with open and send. For example, Abort can be used to cancel any current request.
Steps to do
Step 1: Add internet privilege in config.xml
<tizen:privilege name="http://tizen.org/privilege/internet"/>
Step 2: XmlHttpRequest instance is initialized. After initialization, open method of XmlHttpRequest is called. This method needs three parameters (Request type, SOAP service URL and Async type).
var xmlhttp = new XMLHttpRequest(); xmlhttp.open("POST", "http://www.webservicex.net/country.asmx/GetCountries",true);
Step 3: Create SOAP message in a way for getting service of GetCountries() method.
var soap = '<?xml version="1.0" encoding="utf-8"?>' + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' + 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' + 'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + '<soap:Body> ' + '<GetCountries xmlns="http://www.webserviceX.NET/">' + '</GetCountries>' + '</soap:Body>' + '</soap:Envelope>';
Step 4: Send the request with this soap message as parameter.
xmlhttp.send(soap);
Step 5: Wait until the readystate value is 4. The responseText is carrying text value of the XML. Here the rendering is done on XML as text in HTML.
xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState === 4) { document.getElementById("ws_data").innerHTML = xmlhttp.responseText; } };
Demo: Now, build and run the attached example. This example above is capable of showing the return value of GetCountries() method from http://www.webserviceX.NET. Please see below for screenshot.
Figure 2 : Sample Output
References:
[1] http://www.w3schools.com/xml/xml_soap.asp
[2] http://www.w3schools.com/xml/dom_http.asp
[3] https://www.w3.org/TR/2012/WD-XMLHttpRequest-20120117/
[4] https://en.wikipedia.org/wiki/SOAP
[5] https://www.w3.org/TR/2012/WD-XMLHttpRequest-20120117/#request