Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


Writing all controls of given component to TXMLDocumentFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi 6.x (or higher)
Category:
XML
Skill Level:
Scoring:
Last Update:
03/18/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet XML
Times Scored:
2
Visits:
6807
Uploader: Nik Ozniev
Company:
Reference: N/A
 
Question/Problem/Abstract:
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:

  for k:=0 to MyComponent.ComponentCount-1 do
    begin
      if not(MyComponent.Components[k]  is TControl) then
        Continue;
      // Here create node of the XML-document
    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.

// Writing all controls of given component to TXMLDocument 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;

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





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
L. Rosenstein
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)