Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

星期五, 10月 05, 2007

XML自定規則的建檔及讀取方法



新增4個TButton,並點兩下加入事件。

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, XMLIntf, msxmldom, XMLDoc, XmlTool;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
//自定規則的建檔方法1
procedure TForm1.Button1Click(Sender: TObject);
var
XD: IXMLDocument;
begin
XD := NewXMLDocument;
try
XD.StandAlone := 'yes';
XD.Encoding := 'UTF-8';
XD.Options := [doNodeAutoIndent];
XD.NodeIndentStr := #9;

//在這裡用with來寫反而比較清楚
with XD.AddChild('Family') do
begin
AddChild('Father').NodeValue := 'Bill Gates'; //子標籤
AddChild('Mother').NodeValue := 'Lin Chiling'; //子標籤
with AddChild('My') do //子標籤
begin
Attributes['Name'] := 'Mouse'; //屬性
Attributes['Age'] := 25; //屬性
with AddChild('Wife') do //子子標籤
begin
NodeValue := 'Cat';
Attributes['Age'] := 23; //屬性
end;
with AddChild('Child') do //子子標籤
begin
NodeValue := 'Mouse II';
Attributes['Age'] := 1; //屬性
end;
with AddChild('Child') do //子子標籤
begin
NodeValue := 'Mickey Mouse';
Attributes['Age'] := 15; //屬性
end;
with AddChild('Child') do //子子標籤
begin
NodeValue := 'Donald Duck';
Attributes['Age'] := 14; //屬性
end;
end;
with AddChild('Uncle') do //子標籤
begin
Attributes['Age'] := 35; //屬性
Attributes['Sex'] := 'Male'; //屬性
AddChild('Wife').NodeValue := 'Jolin'; //子子標籤
end;
end;
XD.SaveToFile('C:\Test.xml'); //存檔
showmessage('存檔完成');
finally
XD := nil; //釋放
end;
end;

//自定規則的建檔方法2
procedure TForm1.Button2Click(Sender: TObject);
var
XT: TXmlTool;
begin
XT := TXmlTool.Create;
try
XT['/Family//Father'] := 'Bill Gates'; //目前目錄 /Family//
XT['Mother'] := 'Lin Chiling'; //目前目錄 /Family//
XT['My//@Name/'] := 'Mouse'; //目前目錄 /Family//MY// 其屬性
XT['@Age'] := 25;
XT['Wife//'] := 'Cat'; //目前目錄 /Family//MY//Wife//
XT['@Age'] := 23;
XT['../Child//'] := 'Mouse II'; //目前目錄 /Family//MY//Child//
XT['@Age'] := 1;
XT['../Child[1]//'] := 'Mickey Mouse'; //目前目錄 /Family//MY//Child// 第2個相同名字
XT['@Age'] := 15;
XT['../Child[2]//'] := 'Donald Duck'; //目前目錄 /Family//MY//Child// 第3個相同名字
XT['@Age'] := 14;
XT['../../Uncle//@Age'] := 35; //目前目錄 /Family//Uncle// 其屬性
XT['@Sex'] := 'Male';
XT['Wife//'] := 'Jolin'; //目前目錄 /Family//Uncle//Wife// 其屬性
//目錄最後可以不寫//; 如XT['Wife//']->XT['Wife']; 但外加屬性的不行XT['../../Uncle//@Age'];
XT.SaveToFile('C:\Test.xml');
showmessage('存檔完成');
finally
XT.Free;
end;
end;

//自定規則的讀取方法1
procedure TForm1.Button3Click(Sender: TObject);
var
XD: IXMLDocument;
i: Integer;
begin
XD := LoadXMLDocument('C:\Test.xml');
try
ShowMessage(XD.ChildNodes['Family'].ChildValues['Father']); //內容
ShowMessage(XD.ChildNodes['Family'].ChildValues['Mother']); //內容
ShowMessage(XD.ChildNodes['Family'].ChildNodes['My'].Attributes['Name']); //屬性值
ShowMessage(XD.ChildNodes['Family'].ChildNodes['My'].Attributes['Age']); //屬性值
ShowMessage(XD.ChildNodes['Family'].ChildNodes['My'].ChildValues['Wife']); //內容
ShowMessage(XD.ChildNodes['Family'].ChildNodes['My'].ChildNodes['Wife'].Attributes['Age']); //屬性值
for i := 0 to XD.ChildNodes['Family'].ChildNodes['My'].ChildNodes.Count-1 do
if XD.ChildNodes['Family'].ChildNodes['My'].ChildNodes[i].NodeName = 'Child' then //如果是Child
begin
ShowMessage(XD.ChildNodes['Family'].ChildNodes['My'].ChildNodes[i].NodeValue); //內容
ShowMessage(XD.ChildNodes['Family'].ChildNodes['My'].ChildNodes[i].Attributes['Age']); //屬性值
end;
ShowMessage(XD.ChildNodes['Family'].ChildNodes['Uncle'].Attributes['Age']); //屬性值
ShowMessage(XD.ChildNodes['Family'].ChildNodes['Uncle'].Attributes['Sex']); //屬性值
ShowMessage(XD.ChildNodes['Family'].ChildNodes['Uncle'].ChildValues['Wife']); //內容
finally
XD := nil;
end;
end;

//自定規則的讀取方法2
procedure TForm1.Button4Click(Sender: TObject);
var
XT: TXmlTool;
begin
XT := TXmlTool.Create('C:\Test.xml');
try
ShowMessage(XT['/Family//Father']); //內容
ShowMessage(XT['Mother']); //內容
if XT.SubNodes['My/'].First then
repeat
ShowMessage('1. '+XT.SubNodes['My/'].NodeXPath + '=' + XT.SubNodes['My/'].NodeValue); //My Name、Age及Wife、Child值
if XT.SubNodes['My/'].SubNodes.First then
repeat
ShowMessage('2. '+XT.SubNodes['My/'].NodeXPath + '=' + XT.SubNodes['My/'].SubNodes.NodeValue); //Wife、Child的Age值
until not XT.SubNodes['My/'].SubNodes.Next;
until not XT.SubNodes['My/'].Next;
ShowMessage(XT['Uncle//@Age']); //屬性
ShowMessage(XT['@Sex']); //屬性
ShowMessage(XT['Wife']); //內容
finally
XT.Free;
end;
end;

end.

沒有留言: