با استفاده از این مقاله و کدهای ارائه شده در آن یاد خواهید گرفت که مشخصات کمپوننتها را در یک فایل XML بنویسید. این مقاله از جهاتی بسیار جالب است. شاید هیچ گاه نوشتن مشخصات و خود کمپوننت در یک فایل XML مورد نیاز شما نباشد. ولی با خواندن این مقاله طریقه استفاده از TXMLDocument را در عمل یاد می گیرید.
شیوه کلی کار تقریبا مشخص است:
begin
if not(MyComponent.Components[k] is TControl) then
Continue;
// Here create node of the XML-document
end;
اما مشکل این جاست که باید در فایل XML ساختار پدر – فرزندی در حین ساخت گزینه ها رعایت شود. کد ارائه شده در این مقاله کاملا کار می کند و شما می توانید برای انجام این کار از آن استفاده کنید:
according parent-child relations
Procedure Get_Component_as_XML
(AXMLDoc : TXMLDocument; AComponent : TComponent);
Var
k : Integer;
Ctrl : TControl;
List : TList;
XMLNode : IXMLNode;
GrCtrl : TGraphicControl;
Procedure Add_WinControlAttributes(AWCtrl :
TWinControl; AXMLNode : IXMLNode);
begin
with AXMLNode do
begin
Attributes['Name'] := AWCtrl.Name;
Attributes['Owner'] := AWCtrl.Owner.Name;
Attributes['Left'] := IntToStr(AWCtrl.Left);
Attributes['Top'] := IntToStr(AWCtrl.Top);
Attributes['Width'] := IntToStr(AWCtrl.Width);
Attributes['Height'] := IntToStr(AWCtrl.Height);
// … add all needed properties
end;
end;
Procedure Add_GraphicsControlAttributes(AGCtrl : TGraphicControl;
AXMLNode : IXMLNode);
begin
with AXMLNode do
begin
Attributes['Name'] := AGCtrl.Name;
Attributes['Owner'] := AGCtrl.Owner.Name;
Attributes['Left'] := AGCtrl.Left;
Attributes['Top'] := IntToStr(AGCtrl.Top);
Attributes['Width'] := IntToStr(AGCtrl.Width);
Attributes['Height'] := IntToStr(AGCtrl.Height);
// … add all needed properties
end;
end;
Procedure Add_XML_Node(AWCtrl : TWinControl; AXMLNode : IXMLNode);
Var
i : Integer;
WinC : TWinControl;
SubIXMLNode,
NewStock : IXMLNode;
SubCtrl : TControl;
SubGrCtrl : TGraphicControl;
begin
NewStock := AXMLNode.AddChild(AWCtrl.ClassName);
Add_WinControlAttributes(AWCtrl, NewStock);
if AWCtrl.ControlCount > 0 then
for i:=0 to AWCtrl.ControlCount-1 do
begin
SubCtrl := TControl(AWCtrl.Controls[i]);
if List.IndexOf(SubCtrl) >= 0 then
Continue;
// take a special look at this list
List.Add(Pointer(SubCtrl));
if SubCtrl is TGraphicControl then
begin
SubGrCtrl := TGraphicControl(SubCtrl);
SubIXMLNode := NewStock.AddChild(SubGrCtrl.ClassName);
Add_GraphicsControlAttributes(SubGrCtrl, SubIXMLNode);
end
else if SubCtrl is TWinControl then
begin
WinC := TWinControl(SubCtrl);
Add_XML_Node(WinC, NewStock);
end;
end;
end;
begin
AXMLDoc.XML.Clear;
AXMLDoc.XML.Add(Format(‘<%s>’, [AComponent.Name]));
AXMLDoc.XML.Add(Format(‘</%s>’, [AComponent.Name]));
AXMLDoc.Active := True;
List := TList.Create; // special list to hold controls added to XML
try
for k:=0 to AComponent.ComponentCount-1 do
begin
if not(AComponent.Components[k] is TControl) then
Continue;
Ctrl := TControl(AComponent.Components[k]);
if List.IndexOf(Ctrl) >= 0 then
Continue;
// take a special look at this list
List.Add(Pointer(Ctrl));
if Ctrl is TGraphicControl then
begin
GrCtrl := TGraphicControl(Ctrl);
XMLNode := AXMLDoc.DocumentElement.AddChild(GrCtrl.ClassName);
Add_GraphicsControlAttributes(GrCtrl, XMLNode);
end
else if Ctrl is TWinControl then
// Recursive process for child controls
Add_XML_Node(TWinControl(Ctrl), AXMLDoc.DocumentElement);
end;
finally
List.Clear;
List.Free;
end;
end;
همان طور که گفتم شاید اصولا نیاز به انجام این کار آن چنان وجود نداشته باشید ولی با استفاده از این مقاله شما به خوبی یاد خواهید گرفت که از TXMLDocument استفاده کنید.
انشاء الله در آینده مقالات دیگری را برای آشنایی با کمپوننت فوق العاده جذاب و قوی TXMLDocument ارائه خواهم داد.
موفق و موید و منصور باشید
معموری
دیدگاه خود را بیان کنید.
باید وارد سایت شده باشید برای دیدگاه دادن