在 Delphi 裡處理 XML,實務上最常見的有兩條路:一是使用 Delphi 內建的 XML Broker 家族,二是直接呼叫微軟的 msxml.dll。前者偏向 MIDAS/DataSnap 的多層應用資料傳輸,後者則是通用的 XML 文件操作。
XML Broker 屬於 Delphi 內建元件,通常搭配 Web 應用與 MIDAS 使用。以下是一個自訂的「顯示 XML」按鈕元件範例,示範如何從 TXMLButton 衍生出自己的按鈕,並在產出 HTML 時把 XML 相關的變數名稱一併寫入。
unit ShowXML;
interface
uses
Classes, HTTPApp, Db, DbClient, Midas,
XMLBrokr, WebComp, MidItems;
type
TCustomShowXMLButton = class(TXMLButton, IScriptComponent)
protected
XMLMethodName: string;
{ IScriptComponent }
procedure AddElements(AddIntf: IAddScriptElements);
function GetSubComponents: TObject;
{ IWebContent implementation }
function ImplContent(Options: TWebContentOptions;
ParentLayout: TLayout): string; override;
end;
TShowXMLButton = class(TCustomShowXMLButton)
public
constructor Create(AOwner: TComponent); override;
published
property Custom;
property Style;
property StyleRule;
property Caption;
property XMLBroker;
property XMLUseParent;
end;
TShowDeltaButton = class(TCustomShowXMLButton)
public
constructor Create(AOwner: TComponent); override;
published
property Custom;
property Style;
property StyleRule;
property Caption;
property XMLBroker;
property XMLUseParent;
end;
implementation
uses
SysUtils, MidProd;
resourcestring
sShowXML = 'Show XML';
sShowDelta = 'Show Delta';
procedure TCustomShowXMLButton.AddElements(AddIntf: IAddScriptElements);
begin
AddIntf.AddIncludeFile('XMLshow.js');
end;
function TCustomShowXMLButton.GetSubComponents: TObject;
begin
Result := nil;
end;
function TCustomShowXMLButton.ImplContent(Options: TWebContentOptions;
ParentLayout: TLayout): string;
var
Attrs: string;
Intf: ILayoutWebContent;
FormVarName, RowSetVarName: string;
begin
AddQuotedAttrib(Attrs, 'NAME', Name);
AddQuotedAttrib(Attrs, 'STYLE', Style);
AddQuotedAttrib(Attrs, 'CLASS', StyleRule);
AddQuotedAttrib(Attrs, 'VALUE', Self.Caption);
AddCustomAttrib(Attrs, Custom);
if Assigned(XMLData.XMLBroker) then
begin
FormVarName := XMLData.XMLBroker.SubmitFormVarName;
RowSetVarName := XMLData.XMLBroker.RowSetVarName(nil);
end;
if not (coNoScript in Options.Flags) then
Result := Format('<input type="button" %s onclick="%s.%s.%s(%s)" />',
[Attrs, FormVarName, RowSetVarName, XMLMethodName, sXMLReadyVar])
else
Result := Format('<input type="button" %s />', [Attrs]);
if Assigned(ParentLayout) and
ParentLayout.GetInterface(ILayoutWebContent, Intf) then
Result := Intf.LayoutButton(Result, GetLayoutAttributes);
end;
{ TShowXMLButton }
constructor TShowXMLButton.Create(AOwner: TComponent);
begin
inherited;
DefaultCaption := sShowXML;
XMLMethodName := 'root';
end;
{ TShowDeltaButton }
constructor TShowDeltaButton.Create(AOwner: TComponent);
begin
inherited;
DefaultCaption := sShowDelta;
XMLMethodName := 'getDelta()';
end;
end.
這個元件家族的關鍵在於 XMLMethodName:一般按鈕呼叫 root() 取得整份資料集,Delta 按鈕則呼叫 getDelta() 只取回異動部分。因為 Delphi 會把 Broker 產生的表單變數名與資料列集變數名一併寫進 onclick,客戶端 JavaScript 才能準確地把請求導向正確的資料來源。
如果你要的不是資料集傳輸,而是通用的 XML 文件讀寫、查詢與轉換,那麼直接透過 OLE/COM 呼叫 msxml.dll 會更直接。以下以 DHTML 編輯器元件為例,示範載入、儲存與另存的流程,同樣是透過 OleVariant 傳遞可選參數:
procedure TDHEDForm.OpenBtnClick(Sender: TObject);
var
pVIn, Prompt: OleVariant;
begin
pVIn := '';
Prompt := True;
DHtmlEdit1.LoadDocument(pVIn, Prompt);
end;
procedure TDHEDForm.SaveBtnClick(Sender: TObject);
var
vo, vb: OleVariant;
begin
vo := DHtmlEdit1.CurrentDocumentPath;
if vo <> '' then
vb := False // 已有路徑,直接覆寫
else
begin
vo := '';
vb := True; // 沒有路徑,跳出另存對話框
end;
DHtmlEdit1.SaveDocument(vo, vb);
end;
procedure TDHEDForm.SaveAsClick(Sender: TObject);
var
vo, vb: OleVariant;
begin
vo := '';
vb := True; // 強制另存新檔
DHtmlEdit1.SaveDocument(vo, vb);
end;
如果你的資料來源是 MIDAS/DataSnap 的 provider,並且需要把資料集差異回傳伺服器,XML Broker 是最省事的选择;如果你需要自己組裝 XML 結構、跑 XPath 查詢或做 XSLT 轉換,就該直接掌握 msxml.dll,並透過 Import Type Library 引入 MSXML_TLB,用 IXMLDOMDocument 這類介面操作文件樹。