delphi7下调用微软的Web Services的心得
我在delphi7下調用微軟的Web Services的心得.(其中服務是指返回數據集)
我在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! Rate It
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)privateFs_schema: String;publishedproperty s_schema: String read Fs_schema write Fs_schema;end;ZipCodesSoap = interface(IInvokable)[]...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); beginSOAPResponse.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:
{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); varXMLDoc: IXMLDocument; beginSOAPResponse.Position := 0;Memo1.Lines.LoadFromStream(SOAPResponse);ClientDataset1.Active := FALSE;SOAPResponse.Position := 0;XMLDoc := NewXMLDocument;XMLDoc.Encoding := SUTF8; 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 閱讀(1279) 評論(1) ?編輯?收藏 收藏至365Key
評論
?re: 我在delphi7下調用微軟的Web Services的心得.(其中服務是指返回數據集) ??
最近有個朋友看了我的文章,他說他也在走這條路。不過此時我已經放棄delphi了,轉向C#了。要說的是,我們轉型時間大概兩周,目前已經可以用C#實現基本功能了。可見轉型不太難.
轉載于:https://www.cnblogs.com/fuyingke/archive/2005/09/02/228400.html
總結
以上是生活随笔為你收集整理的delphi7下调用微软的Web Services的心得的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常用基类名称空间
- 下一篇: 世界上最遥远的距离(泰戈尔)