Where in the World is Sven Svensson?

By: Anders Ohlsson

Abstract: Web service consumption and XML transforms made easy

I just found the coolest web service listed on www.xmethods.org. Well, the coolest would be my implementation of the BabelFish service. OK, so this one is the second coolest one on www.xmethods.org. It's the Swedish Phone Book. Now I'll never have to bring another 50 lbs worth of phone books on a plane again just to keep updated on my friends back in Sweden... ;-)

This article uses Delphi 6.02, but it's just as easy to implement the demo in C++Builder 6 or Kylix 2. The source code for this demo can be downloaded from CodeCentral.

The web service is called Search Swedish Persons. It's written in Delphi by Henry Aspenryd (with Marotz AB in Sweden), and the WSDL is found right here.

Step 1: Importing the web service

Importing the WSDL for any web service is very simple. Just select File | New | Other | WebServices | WSDL Importer and specify the WSDL location as http://www.marotz.se/scripts/searchperson.exe/wsdl/ISearchSwedishPerson, hit Next and Finish, save the unit (or use mine from the source code mentioned above) and you're done.

If you wanted to test the service really quick, you could drop a TButton, and enter one line of code in it's event:

  ShowMessage(GetISearchSwedishPerson.XMLSearchAddress('Anders','Ohlsson','','',''));

If you ran this simple app and clicked on the button, you'd see a huge dialog after a few seconds that contains some 190 hits. And here you thought my name was unique. ;-)

Write a line or two more and save the search result to a file, and then view it in Internet Explorer or something that will view XML files nicely. Then you would see that there's actually some kind of order to this chaos.


A sample XML result set.

But what do we do if we want to see the result set in a grid? The answer is in the XML Mapper tool that comes with Delphi 6 Enterprise.

Step 2: Transforming the data

Select Tools | XML Mapper. Now you're in the XMP mapping tool. Select File | Open and point it to phone.xml included with the source for this article. You should now see the XMl structure pop up on the left side. Select the "Entry" node (not the "Entries" node). Right click on this node and select Save All Children. Now the left side of the transform in the middle should be populated. Select Create | Datapacket from XML. Now the second half of the transform should be done, and your screen should look something like this:


The XML Mapper and our transform.

Hit the button labeled "Create and Test Transformation". You should now see something like this:


The datapacket viewed in the XML Mapper.

In order to reuse the transform we just created, save it off by selecting File | Save | Transformation. I saved mine as XMLtoData.xml. You can save it as Foo.xtr if you want, I used the xml extension so that I can view it in IE.


The transform file viewed in a browser.

Step 3: Putting it all together


The demo client showing a datapacket in a grid.

The main part of the demo (above) consists of a TDBGrid, a TDataSource and a TClientDataSet, but the most important component here is the TXMLTransformProvider that transforms any XML result set from the Swedish phone book service. Let's look at the meat of the code:

  if ws = nil then
    ws := GetISearchSwedishPerson;
  SL := TStringList.Create;
  SL.Text := ws.XMLSearchAddress(leFirstName.Text,leLastName.Text,leAddress.Text,leZipCode.Text,leCity.Text);
  SL.SaveToFile(ResultFileName);
  SL.Free;
  XMLTransformProvider1.TransformRead.TransformationFile := XformFileName;
  XMLTransformProvider1.XMLDataFile := ResultFileName;
  ClientDataSet1.Close;
  ClientDataSet1.Open;

This snippet of code calls the web service to retrieve the result set matching the search parameters we've keyed into the edit boxes, then saves that result set (XML) to a file on disk (phone.xml), then it hooks our XMLTransformProvider up with the transform file (XMLtoData.xml) and to the results set itself (phone.xml). Finally the code opens the client dataset and the rest of the magic happens automatically.

Yet again, we have proven that Delphi makes the hard things easy... ;-)


Server Response from: ETNASC04