| 維度 | XML Broker (Delphi 內建) | MSXML (COM 組件) |
|---|---|---|
| 部署 | 無需額外 DLL,編譯進 EXE | 需目標機安裝 MSXML (Win 內建較新版) |
| 編程模型 | 組件化、設計時綁定、WebSnap 架構 | DOM/SAX 標準 API、晚綁定/早綁定均可 |
| 適用場景 | WebSnap 服務端、XML 按鈕、Delta 包顯示 | 通用 XML 解析、生成、轉換、XPath、XSLT |
| 學習曲線 | 較陡,文檔稀缺 | 平緩,資源豐富,跨語言通用 |
| 現代建議 | 僅維護舊 WebSnap 項目 | 首選,或升級至 OmniXML/SimpleXML 等純 Pascal 庫 |
核心單元:XMLBrokr、MidItems、WebComp。適用於構建輸出 XML 數據的 Web 服務端控件。
TXMLBroker — 管理 XML 數據包 (DataPacket) 與客戶端交互
├─ TXMLButton — 渲染「顯示 XML」按鈕
│ ├─ TShowXMLButton — 顯示完整 XML
│ └─ TShowDeltaButton — 顯示增量變更
└─ IScriptComponent — 生成客戶端 JS (xmlshow.js)
unit ShowXML;
interface
uses Classes, HTTPApp, DB, DbClient, Midas, XMLBrokr, WebComp, MidItems;
type
TCustomShowXMLButton = class(TXMLButton, IScriptComponent)
protected
XMLMethodName: string;
procedure AddElements(AddIntf: IAddScriptElements);
function GetSubComponents: TObject;
function ImplContent(Options: TWebContentOptions; ParentLayout: TLayout): string; override;
end;
TShowXMLButton = class(TCustomShowXMLButton)
public constructor Create(AOwner: TComponent); override;
published property XMLBroker; property XMLUseParent; property Caption; ...
end;
TShowDeltaButton = class(TCustomShowXMLButton)
public constructor Create(AOwner: TComponent); override;
published property XMLBroker; property XMLUseParent; property Caption; ...
end;
implementation
uses SysUtils, MidProd;
procedure TCustomShowXMLButton.AddElements(AddIntf: IAddScriptElements);
begin
AddIntf.AddIncludeFile('xmlshow.js'); // 客戶端 JS
end;
function TCustomShowXMLButton.ImplContent(...): string;
var Attrs, FormVarName, RowSetVarName: string;
begin
// 構建按鈕 HTML + JS 調用參數
// XMLMethodName = 'root' (完整) 或 'getDelta()' (增量)
// 輸出
end;
constructor TShowXMLButton.Create(AOwner: TComponent);
begin
inherited; DefaultCaption := 'Show XML'; XMLMethodName := 'root';
end;
constructor TShowDeltaButton.Create(AOwner: TComponent);
begin
inherited; DefaultCaption := 'Show Delta'; XMLMethodName := 'getDelta()';
end;
通過 MSXML2_TLB (早綁定) 或 CreateOleObject('MSXML2.DOMDocument') (晚綁定) 使用。現代 Delphi 建議引用 Winapi.Msxml 單元進行早綁定開發。
uses ComObj, MSHTML; // 或 Winapi.Msxml
procedure TForm1.LoadXML(const FileName: string);
var
Doc: OleVariant; // IXMLDOMDocument2
begin
Doc := CreateOleObject('MSXML2.DOMDocument.6.0'); // 版本無關 ProgID
Doc.async := False;
Doc.validateOnParse := False;
if Doc.load(FileName) then
begin
// 遍歷節點
TraverseNode(Doc.documentElement, 0);
end
else
ShowMessage('解析錯誤: ' + Doc.parseError.reason);
end;
procedure TraverseNode(Node: OleVariant; Level: Integer);
var
i: Integer;
Child: OleVariant;
begin
// 簡單輸出節點名與文本
Memo1.Lines.Add(StringOfChar(' ', Level) + Node.nodeName + ': ' + Node.text);
for i := 0 to Node.childNodes.length - 1 do
TraverseNode(Node.childNodes.item[i], Level + 1);
end;
uses Winapi.Msxml, System.Win.ComObj;
procedure TForm1.ParseXMLEarly(const FileName: string);
var
Doc: IXMLDOMDocument2;
NodeList: IXMLDOMNodeList;
Node: IXMLDOMNode;
Attr: IXMLDOMAttribute;
begin
Doc := CoDOMDocument60.Create; // 早綁定,代碼提示、類型檢查
Doc.async := False;
Doc.setProperty('SelectionLanguage', 'XPath'); // 啟用 XPath
if not Doc.load(FileName) then
raise Exception.Create('XML 載入失敗: ' + Doc.parseError.reason);
// XPath 查詢所有 book 節點
NodeList := Doc.selectNodes('//book');
for Node in NodeList do
begin
Attr := Node.attributes.getNamedItem('id');
if Assigned(Attr) then
Memo1.Lines.Add('Book ID: ' + Attr.text);
// 訪問子元素
Memo1.Lines.Add(' Title: ' + Node.selectSingleNode('title').text);
Memo1.Lines.Add(' Price: ' + Node.selectSingleNode('price').text);
end;
end;
避免:新項目使用 XML Broker (WebSnap 已棄用)、舊版 MSXML 3/4 (安全風險)、手工字串拼接 XML。
Delphi 處理 XML 歷經兩代:XML Broker 服務於 WebSnap 時代的服務端組件化輸出;MSXML 提供通用 DOM/SAX/XPath 能力。現代開發**首選純 Pascal 庫 (OmniXML/SimpleXML)**,零部署、跨平台、類型安全。若必須用 MSXML,請用 MSXML2.DOMDocument.6.0 + 早綁定 Winapi.Msxml,啟用 XPath 大幅簡化查詢代碼。