cowbird 心有多大,世界有多大

燕八哥 MSN:cowbird2002@hotmail.com

know everything about something and something about everything

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

  我在delphi7下调用微软的Web Services的心得.(其中服务是指返回数据集)
        作者:cowbird                 日期:20040619

       本来是想写一篇文章,用来表达我对delphi7下调用web services的失望,因为尝试了不少时间,怎么也不能实现用delphi7调用dotnet下的返回数据集的web服务。
        今天做了次最后的尝试,没有想到,倒是成功了。
  调用方法是看了一篇老外的文章。经过我的实验,证实老外文章中方法是正确的,可恶的是很多小地方没有提及,花了我不少时间,这里我用红色字写出来,希望大家不要再走我的弯路。
  delphi调用返回数据集web services的方法:
  delphi通过httpRIO的控件,来接收soap。然后把soap写成xml文件,通过外部工具xml mapper生成一个xtr翻译文件。这个xtr就做为XMLTransformProvider的transformFile(翻译文件)。以后只要httpRIO传来的soap传给xmlTransformProvider后,xmTransformProvider就可以通过翻译文件(xtr)得到数据,提供给clientDataSet。
        是不是觉得很麻烦,dotnet里面dataset可以直接接住数据。不过我个人认为delphi还残留一点优势,毕竟win32程序从开始淘汰到正式淘汰还有漫长的岁月。就算longhorn系统出现,也仍有虚拟机来跑win32程序。那么使用delphi7结合web services开发,一方面可以暂时省去学习其他开发工具的时间,另一方面使用web services也可以为以后开发重用。如果项目时间紧,开发成员对vs.net开发环境不熟悉,倒是一种过渡性的开发方式。

下面文章摘于http://community.borland.com/article/0,1410,28631,00.html

Use ADO.NET datasets in Delphi
Ratings: be the first! Not Rated Rate It

Abstract: Learn how to use ADO.NET datasets in Delphi, using XML mapper to transform XML across the platforms. Demonstrated using a .NET web service and Delphi client. 

Use ADO.NET Datasets in Delphi


By Deepak Shenoy

Introduction

If you've experimented with Web Services, you might have hit some Microsoft .NET based Web Services which return data all right, but it's in the default XML format from ADO.NET. So you end up with some XML but you have no clue what to do with it! This article explains how you can take this XML, make sense out of it and even display it in a DB Grid.

Scope

I'm not going to explain much about .NET, or the ADO.NET XML format. What I'll talk about is the most probable case you'll encounter .NET datasets: as XML returned from a .NET based Web Service. If you're going to use .NET datasets in some other way, you might want to read this article to get an idea of how to make your Delphi application aware of them.

Hitting the .NET service

Let's start with a simple .NET service, as given in http://services.pagedownweb.com/ZipCodes.asmx. I've used the Web Service Importer in File | New | Other | Web Services and generated the Pascal files. Here's the declaration that looks odd:


  rtnZipDSResult = class(TRemotable)
  private
    Fs_schema: String;
  published
    property s_schema: String read Fs_schema write Fs_schema;
  end;

  ZipCodesSoap = interface(IInvokable)
  ['{FEF279A0-29EE-CF0B-FBB2-7DD79A5502CE}']
    ...
	function  rtnZipDS(const City_IN: String; const State_IN: String): rtnZipDSResult; stdcall;
	...
  end;
The rtnZipDS function returns a .NET dataset, as XML. Here, the s_schema of the rtnZipDSResult class is simply the dataset as XML in ADO.NET's default format. This means that a .NET client could easily get this XML and show it on a grid or a form - but can we do this with Delphi? Let's see.

Using the .NET service in Delphi

I've created a sample form , which looks like this:

Now we've setup the HTTPRio, and the code behind the Get Zip Codes button is:


procedure TForm1.Button1Click(Sender: TObject);
begin
  (HTTPRIO1 as ZipCodesSoap).rtnZipDS(edtCity.Text, edtState.Text);
end;
I've also added an event handler on the HTTPRio1.OnAfterExecute like so:

procedure TForm1.HTTPRIO1AfterExecute(const MethodName: String;
  SOAPResponse: TStream);
begin
  SOAPResponse.Position := 0;
  Memo1.Lines.LoadFromStream(SOAPResponse);

  SOAPResponse.Position := 0;
end;

This is only to display the returned content on to a Memo so we can figure out what to do with it. Here's how the form looks now:

Interpreting the .NET XML

We have to figure out how to get Delphi to USE this data. We would like to have a Client Data Set read the XML so we can display it all in a Grid. For that we'll have to use XDR transforms. No, that's not very complicated, and here's how we'll do it.

1. First we're going to save the XML returned into an XML file. I've saved it as "data.xml".
2. Run XML mapper from the Tools menu, and open this XML file. Here's a mega screen shot:

{用C#开发web services的时候,如果你这样写  oleDbDataAdapter1.Fill(ds,'tablename');}那么你是看不到上面橙色筐中的字段的。千万不要表明数据集中表名。你这样写就可以了,oleDbDataAdapter1.Fill(ds);就能显示字段了!

3. The ZIPDATA(*) means there's multiple rows of "ZIPDATA" available. Columns availabl are Zip, City, State, County and AreaCode. Let's double-click each one of these to add them to the transformation and then click the DataPacket from XML in the Create Menu. Here's what it all looks like:

4. Save the Transformation using File | Save | Transformation, as "Ziptrans.xtr". Don't try to test the transformation yet. (There's a bug in Delphi Source code that doesn't like SOAP namespaces in certain elements so it doesn't show up any data).

5. We'll now FIX this bug. The XTR file is an XML file which you can open in any text editor. Open it, and change the first line from:


<SelectEach dest="DATAPACKETROWDATAROW" from="soap:Envelopesoap:Body....">

   [Change To]

<SelectEach dest="DATAPACKETROWDATAROW" from="Envelopesoap:Body....">
The reason for this is that Delphi's XML Transform provider does not like the "soap:" in the first element of the "from" attribute. That might get fixed in some update pack, so this point might not apply

{from后面除了soap:,只要是单词后面有冒号的,该单词和冒号都要去掉,delphi才能显示数据}

6. We're nearly there. Drop a TClientDataset, a TXMLTransformProvider and a TDatasource on the form. Here's what the form looks like now:

Link the Grid, the Datasource and the ClientDataset, and set the ClientDataset's ProviderName to point to the XML Transform Provider. 7. Set the TransformRead.TransformationFile of the XMLTransformProvider to Ziptrans.XTR.

8. Now we need to set the data of the XML Transform Provider at run time. Here's some additional code in the HTTPRio's OnAfterExecute:


procedure TForm1.HTTPRIO1AfterExecute(const MethodName: String;
  SOAPResponse: TStream);
var
  XMLDoc: IXMLDocument;
begin
  SOAPResponse.Position := 0;
  Memo1.Lines.LoadFromStream(SOAPResponse);

  ClientDataset1.Active := FALSE;
  SOAPResponse.Position := 0;
  XMLDoc := NewXMLDocument;
  XMLDoc.Encoding := SUTF8; //应该是'SUTF8'并需要引用XMLIntf,XMLDoc两个单元
  SOAPResponse.Position := 0;
  XMLDoc.LoadFromStream(SOAPResponse);

  XMLTransformProvider1.TransformRead.SourceXmlDocument := XMLDoc.GetDOMDocument;
  ClientDataset1.Active := TRUE;
end;

You'll notice that we've created an XML Document , loaded it from the Received SOAP stream, and applied the transform to it. The client dataset gets data from the provider and displays the data :

That's it!

Amazing.

Thank you.

What's next?

This transformation is very specific to this particular service and XML schema. SO if you know what XML is going to be returned (the format) then you can use XML mapper to generate a transformation for it.

I haven't been able to write a "general" transform that can be applied to ANY .NET returned XML, but if anyone does I'd love to hear about it.

Also, why have I used the HTTPRio's OnAfterExecute, rather than manipulating the the s_schema parameter? There's another bug in Delphi that doesn't like parameters returned as XML. More revealed in this thread.

You can download all the code for this project at http://codecentral.borland.com/codecentral/ccweb.exe/listing?id=17807 or at http://www.agnisoft.com/soap/dotnetds.zip.

Deepak Shenoy(shenoy@agnisoft.com) is a Director at Agni Software, a software company in India that offers consultancy and offshore development solutions. It might be a while before his hair gets pointy so he's allowed to understand some technology.

posted on 2004-06-19 14:40  cowbird  阅读(5578)  评论(8编辑  收藏  举报