Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to write all controls of given component to TXMLDocument Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
04-Jun-03
Category
XML
Language
Delphi 6.x
Views
152
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			 Author: Nik Ozniev

Down all controls for component in XMLDocument in simplest way?

Answer:

Simply it seems to pass under the component list and to create appropriate nodes of 
the XML-document: 

1   for k := 0 to MyComponent.ComponentCount - 1 do
2   begin
3     if not (MyComponent.Components[k] is TControl) then
4       Continue;
5     // Here create node of the XML-document
6   end;


However the problem is to satisfy parent - child relations during creation of nodes 
of the XML-document. 
The procedure given below carries out this task by conducting special list of those 
controls for which are already created XML-document nodes. 
Controls corresponding to created nodes are excluded from process of iteration. 
7   
8   // Writing all controls of given component to TXMLDocument according parent-child 
9   relations
10  
11  procedure Get_Component_as_XML(AXMLDoc: TXMLDocument; AComponent: TComponent);
12  var
13    k: Integer;
14    Ctrl: TControl;
15    List: TList;
16    XMLNode: IXMLNode;
17    GrCtrl: TGraphicControl;
18  
19    procedure Add_WinControlAttributes(AWCtrl: TWinControl; AXMLNode: IXMLNode);
20    begin
21      with AXMLNode do
22      begin
23        Attributes['Name'] := AWCtrl.Name;
24        Attributes['Owner'] := AWCtrl.Owner.Name;
25        Attributes['Left'] := IntToStr(AWCtrl.Left);
26        Attributes['Top'] := IntToStr(AWCtrl.Top);
27        Attributes['Width'] := IntToStr(AWCtrl.Width);
28        Attributes['Height'] := IntToStr(AWCtrl.Height);
29        // ... add all needed properties
30      end;
31    end;
32  
33    procedure Add_GraphicsControlAttributes(AGCtrl: TGraphicControl; AXMLNode:
34      IXMLNode);
35    begin
36      with AXMLNode do
37      begin
38        Attributes['Name'] := AGCtrl.Name;
39        Attributes['Owner'] := AGCtrl.Owner.Name;
40        Attributes['Left'] := AGCtrl.Left;
41        Attributes['Top'] := IntToStr(AGCtrl.Top);
42        Attributes['Width'] := IntToStr(AGCtrl.Width);
43        Attributes['Height'] := IntToStr(AGCtrl.Height);
44        // ... add all needed properties
45      end;
46    end;
47  
48    procedure Add_XML_Node(AWCtrl: TWinControl; AXMLNode: IXMLNode);
49    var
50      i: Integer;
51      WinC: TWinControl;
52      SubIXMLNode,
53        NewStock: IXMLNode;
54      SubCtrl: TControl;
55      SubGrCtrl: TGraphicControl;
56    begin
57      NewStock := AXMLNode.AddChild(AWCtrl.ClassName);
58      Add_WinControlAttributes(AWCtrl, NewStock);
59  
60      if AWCtrl.ControlCount > 0 then
61        for i := 0 to AWCtrl.ControlCount - 1 do
62        begin
63          SubCtrl := TControl(AWCtrl.Controls[i]);
64          if List.IndexOf(SubCtrl) >= 0 then
65            Continue;
66  
67          // take a special look at this list
68          List.Add(Pointer(SubCtrl));
69  
70          if SubCtrl is TGraphicControl then
71          begin
72            SubGrCtrl := TGraphicControl(SubCtrl);
73            SubIXMLNode := NewStock.AddChild(SubGrCtrl.ClassName);
74            Add_GraphicsControlAttributes(SubGrCtrl, SubIXMLNode);
75          end
76  
77          else if SubCtrl is TWinControl then
78          begin
79            WinC := TWinControl(SubCtrl);
80            Add_XML_Node(WinC, NewStock);
81          end;
82        end;
83    end;
84  
85  begin
86    AXMLDoc.XML.Clear;
87    AXMLDoc.XML.Add(Format('<%s>', [AComponent.Name]));
88    AXMLDoc.XML.Add(Format('</%s>', [AComponent.Name]));
89  
90    AXMLDoc.Active := True;
91  
92    List := TList.Create; // special list to hold controls added to XML
93    try
94      for k := 0 to AComponent.ComponentCount - 1 do
95      begin
96        if not (AComponent.Components[k] is TControl) then
97          Continue;
98        Ctrl := TControl(AComponent.Components[k]);
99        if List.IndexOf(Ctrl) >= 0 then
100         Continue;
101 
102       // take a special look at this list
103       List.Add(Pointer(Ctrl));
104 
105       if Ctrl is TGraphicControl then
106       begin
107         GrCtrl := TGraphicControl(Ctrl);
108         XMLNode := AXMLDoc.DocumentElement.AddChild(GrCtrl.ClassName);
109         Add_GraphicsControlAttributes(GrCtrl, XMLNode);
110       end
111 
112       else if Ctrl is TWinControl then
113         // Recursive process for child controls
114         Add_XML_Node(TWinControl(Ctrl), AXMLDoc.DocumentElement);
115     end;
116   finally
117     List.Clear;
118     List.Free;
119   end;
120 end;


I do not apply for exclusive novelty, but it seems, that this procedure may be useful to someone.

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC