Web DevCenter
oreilly.comSafari Books Online.Conferences.
MySQL Conference and Expo April 14-17, 2008, Santa Clara, CA

Sponsored Developer Resources

Web Columns
Adobe GoLive
Essential JavaScript
Megnut

Web Topics
All Articles
Browsers
ColdFusion
CSS
Database
Flash
Graphics
HTML/XHTML/DHTML
Scripting Languages
Tools
Weblogs

Atom 1.0 Feed RSS 1.0 Feed RSS 2.0 Feed

Learning Lab






Using the Mozilla SOAP API
Pages: 1, 2, 3, 4, 5

Retrieving Parameters

Now you can retrieve the information returned from the SOAP service method. The most direct approach is to use the getParameters method of the SOAPResponse object. As you may have guessed, getParameters returns an array containing any parameters sent back from the SOAP service.

function parseResult(result)
{
    if (result==false)
    {
         return;
    }
    var num = new Object();
    var params = result.getParameters(false,num);
}

The call to getParameters requires two arguments: a Boolean false (indicating that this is an RPC-style message) and a generic JavaScript object num, which will hold the count of parameters returned.

The number of parameters returned depends on the service, and should be described in the WSDL file. In the case of Google, all three service methods return a single parameter as a response. In Google's WSDL file, under "Messages," you can see that the parameter in all three cases is a single RETURN element.

Parsing the Return Values

Retrieving the data from the service is now a simple matter of parsing the parameters. Each parameter in the array has the following properties:

  • name: the name of the parameter.
  • value: the value of the parameter.
  • element: a pointer to the DOM node in the returned SOAP XML message that represents the parameter.

Let's put this knowledge to work. Since the doSpellingSuggestion returns only one parameter containing the suggested spelling, you can refer directly to the value property of the parameter to retrieve the suggestion:

function parseResult(result)
{
    if (result==false)
    {
         return;
    }
    var num = new Object();
    var params = result.getParameters(false,num);
    var suggestion = params[0].value;

    alert("Suggested spelling: " + suggestion);
}

Alternately, you can use the DOM to retrieve the information via the parameter's element property:

var suggestion = params[0].element.firstChild.nodeValue);

The ability to use the DOM in this way is particularly helpful when the parameters return complex XML rather than simple name/value pairs. An example of this is the doGoogleSearch service method, which provides not only rich information about the search results but also meta-information regarding search time, total result count, and filtering. For example, upon receiving a response from the Google search service, you may want to grab only the names and cached file sizes of the matching documents. With the DOM, this is fairly straightforward:

function showResults(results)
{
    if (!results)
    {
        return;
    }
    var params = results.getParameters(false,{});
    var matches = params[0].element.getElementsByTagName("item");
    var str = "";
    for (var i=1;i<matches.length;i++)
    {
      var URL = matches[i].getElementsByTagName("URL").item(0).firstChild;
      var title = matches[i].getElementsByTagName("title").item(0).firstChild;
      var cache = matches[i].getElementsByTagName("cachedSize").item(0).firstChild;

      str += "<a href=\"" + URL.nodeValue + "\">" + title.nodeValue + 
          "</a> (" + cache.nodeValue + ")<br/>";
    }
    document.body.innerHTML = str;
}

Play Along at Home

You can download the complete working example I've created, made with the Google API:

Mozilla Google Search

This covers the entire process, from defining parameters to executing the SOAP call to parsing and displaying the search results with DOM methods. Go ahead and experiment with the code and write your own interfaces to other SOAP services, and be creative when using the returned data in your Web applications.

Looking beyond the browser, the scripting engine is part of the complete Mozilla package, so it's possible to write XUL (XML User-interface Language, on which the Mozilla UI is built) applications that take advantage of the same API. The Mozilla SOAP API allows you to use familiar Web standards - JavaScript and the DOM - to easily integrate Web services into your client-side applications.

Scott Andrew LePera lives in San Francisco, where he ekes out a schizoid existence as both a web applications developer for KnowNow and a frustrated urban folk singer.


Return to the Web Development DevCenter.