先虛擬一個測試文件: test.xml; 放在 c:\temp\ 下備用.
unit Unit1;
interface
uses
Classes, Controls, Forms, StdCtrls, XMLDoc, xmldom, XMLIntf, msXMLdom;
type
TForm1 = class(TForm)
XMLDocument1: TXMLDocument;
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//載入方法1: LoadFromFile
procedure TForm1.Button1Click(Sender: TObject);
begin
XMLDocument1.LoadFromFile('c:\temp\test.XML');
Memo1.Lines := XMLDocument1.XML; {查看}
end;
//載入方法2: 指定 FileName, 然後激活
procedure TForm1.Button2Click(Sender: TObject);
begin
XMLDocument1.FileName := 'c:\temp\test.XML';
XMLDocument1.Active := True; {激活}
Memo1.Lines := XMLDocument1.XML; {查看}
end;
//載入方法3: 指定 LoadFromStream
procedure TForm1.Button3Click(Sender: TObject);
var
ms: TMemoryStream;
begin
ms := TMemoryStream.Create;
ms.LoadFromFile('c:\temp\test.XML');
XMLDocument1.LoadFromStream(ms);
ms.Free;
Memo1.Lines := XMLDocument1.XML; {查看}
end;
//可以用 LoadFromFile 或指定 FileName 的方法, 訪問網上的 XML
procedure TForm1.Button4Click(Sender: TObject);
begin
XMLDocument1.LoadFromFile('http://www.google.com/ig/skins/jr.XML');
Memo1.Lines := XMLDocument1.XML; {查看}
end;
end.
如果需要用浏覽器查看 XML, 需要一個 api 函數: ShellAPI.ShellExecute, 所以先 uses ShellAPI;
然後: ShellExecute(Handle, 'open', 'c:\temp\test.XML', nil, nil, SW_NORMAL);