適用於構建 Web 介面按鈕,點擊顯示 XML 資料/異動內容。核心類別:
TXMLBroker:管理 XML 資料包與資料集同步TCustomShowXMLButton / TShowXMLButton / TShowDeltaButton:產生客戶端 JavaScript 按鈕關鍵介面:IScriptComponent, IWebContent, ILayoutWebContent。
ImplContent 方法輸出 HTML + JavaScript,前端呼叫 XMLBroker 的 root 或 getDelta() 方法。
// 核心輸出邏輯 (簡化)
function TCustomShowXMLButton.ImplContent(...): string;
var Attrs, FormVar, RowSetVar: string;
begin
// 建立按鈕屬性
AddQuotedAttrib(Attrs, 'NAME', Name);
AddQuotedAttrib(Attrs, 'STYLE', Style);
AddQuotedAttrib(Attrs, 'CLASS', StyleRule);
AddQuotedAttrib(Attrs, 'VALUE', Caption);
// 取得 XMLBroker 變數名
if Assigned(XMLData.XMLBroker) then
begin
FormVar := XMLData.XMLBroker.SubmitFormVarName;
RowSetVar := XMLData.XMLBroker.RowSetVarName(nil);
end;
// 輸出含 JavaScript 的 HTML
Result := Format('', [Attrs, FormVar, RowSetVar, XMLMethodName]);
end;
優點:與 DataSnap/ClientDataSet 無縫整合、自動產生前後端同步代碼。
缺點:綁定 WebSnap 架構,學習曲線陡峭,現代 Delphi 專案較少使用。
利用 DHTMLEdit (Microsoft HTML 編輯控件) 載入/儲存 XML 文件,適合簡單檔案型 XML 編輯器。
procedure TForm1.OpenBtnClick(Sender: TObject);
var
VIn, Prompt: OleVariant;
begin
VIn := ''; Prompt := True;
DHTMLEdit1.LoadDocument(VIn, Prompt); // 彈開檔對話框載入
end;
procedure TForm1.SaveBtnClick(Sender: TObject);
var
VPath, VPrompt: OleVariant;
begin
VPath := DHTMLEdit1.CurrentDocumentPath;
if VPath <> '' then VPrompt := False else VPrompt := True;
DHTMLEdit1.SaveDocument(VPath, VPrompt);
end;
procedure TForm1.SaveAsClick(Sender: TObject);
var
VPath, VPrompt: OleVariant;
begin
VPath := ''; VPrompt := True;
DHTMLEdit1.SaveDocument(VPath, VPrompt);
end;
需在專案 Import Type Library 匯入 Microsoft HTML Object Library 與 MSXML,並將 DHTMLEdit 放上表單 (ActiveX 分頁)。
| 需求 | 推薦元件/庫 |
|---|---|
| DOM 解析/查詢 (XPath) | OmniXML, NativeXML, MSXML (IXMLDOMDocument) 晚期綁定 |
| 高效能序列化/反序列化 | XML Data Binding Wizard (產生強型別類別), Alcinoe XML |
| 簡單讀寫設定檔 | TXMLDocument (VCL 封裝 MSXML), SimpleStorage |
| 大型 XML 流式處理 | Sax / XmlLite (Windows 內建) |
WebSnap/XML Broker 適合傳統 WebSnap 專案維護;新專案建議採用獨立 XML 庫,靈活度更高、相依性更低。