首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

Delphi 中使用 XML 文檔的兩種方式:XML Broker vs MSXML

     編輯:Delphi

方案對比概覽

維度XML Broker (Delphi 內建)MSXML (COM 組件)
部署無需額外 DLL,編譯進 EXE需目標機安裝 MSXML (Win 內建較新版)
編程模型組件化、設計時綁定、WebSnap 架構DOM/SAX 標準 API、晚綁定/早綁定均可
適用場景WebSnap 服務端、XML 按鈕、Delta 包顯示通用 XML 解析、生成、轉換、XPath、XSLT
學習曲線較陡,文檔稀缺平緩,資源豐富,跨語言通用
現代建議僅維護舊 WebSnap 項目首選,或升級至 OmniXML/SimpleXML 等純 Pascal 庫

方案一:XML Broker (WebSnap 架構組件)

核心單元: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;

方案二:MSXML (Microsoft XML Core Services)

通過 MSXML2_TLB (早綁定) 或 CreateOleObject('MSXML2.DOMDocument') (晚綁定) 使用。現代 Delphi 建議引用 Winapi.Msxml 單元進行早綁定開發。

晚綁定簡化範例 (原文 DHTMLEdit 相關代碼精簡)


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;

現代 Delphi XML 處理最佳選擇 (2024+)

  1. OmniXML:純 Pascal、無外部依賴、支援 DOM/XPath/XSLT、跨平台 (VCL/FMX/Linux)。
  2. SimpleXML:極簡 API、基於 OmniXML、適合快速腳本。
  3. NativeXML (商業):高性能、流式讀寫、大文件支援。
  4. System.Xml (FireMonkey):跨平台標準庫。

避免:新項目使用 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 大幅簡化查詢代碼。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved