Note
Sorry in advance for the long post, I though it would be best to put as much information on as possible rather than fill the gaps when needed.
Note although I have tagged this as Delphi as well and do own and still use Delphi XE I am now using Lazarus as my primary IDE, I simply cannot afford to purchase the newer Delphi versions and now Lazarus is becoming more stable it makes sense to me to make the switch to Lazarus.
For this question I have included a zip attachment with project source, although written in Lazarus it will really help with the question I have, hence the comments in the first paragraph.
Overview
Onto the question, I have a Object that owns several classes as TLists.
I represent this data in a Treeview and there is no way of knowing how many levels and nodes will be present in the tree as they are dynamically created at runtime. One limitation I have set is that the Top level nodes will be fixed, meaning they cannot be deleted or renamed - these are what I will call RootGroups.
The Treeview will be populated with items and groups, every node added to the Treeview will have its own Object assigned to the data to identify each item correctly. I am going to show an example screenshot now to give a better idea before carrying on:
As you can see I have the two top most nodes, Object1Root and Object2Root. If you notice the buttons on the right, they allow adding group and items to the Treeview but they become disabled if they don't belong in that part of the Treeview. For example you cannot add Object2Group or Object2Item under Object1Root.
Basically everything in the Treeview has its own Pointer to a Object. Each Object I am deriving from a Base Object. This Base Object has properties to store the position of where it is found in the Treeview, like this:
type
TBaseObject = class
private
FName: string;
FGroup: string;
FNodeLevel: Integer;
FNodeIndex: Integer;
public
constructor Create(AName: string);
destructor Destroy; override;
published
property Name: string read FName write FName;
property Group: string read FGroup write FGroup;
property NodeLevel: Integer read FNodeLevel write FNodeLevel;
property NodeIndex: Integer read FNodeIndex write FNodeIndex;
end;
I can then derive my other classes from the Base Object, like this:
type
TObject1RootGroup = class(TBaseObject)
public
constructor Create(AName: string);
destructor Destroy; override;
procedure ToSave(const XMLDoc: IXMLDocument; var Root, Node: IXMLNode);
end;
TObject1Group = class(TBaseObject)
public
constructor Create(AName: string);
destructor Destroy; override;
procedure ToSave(const XMLDoc: IXMLDocument; var Root, Node: IXMLNode);
end;
TObject1Item = class(TBaseObject)
private
FSomeVal1: string;
FSomeVal2: string;
public
constructor Create(AName: string);
destructor Destroy; override;
procedure ToSave(const XMLDoc: IXMLDocument; var Root, Node: IXMLNode);
published
property SomeVal1: string read FSomeVal1 write FSomeVal1;
property SomeVal2: string read FSomeVal2 write FSomeVal2;
end;
The Main Object that holds all these classes looks like this:
type
TMyObject = class(TObject)
private
FName: string;
FObject1Groups: TList;
FObject1Items: TList;
FObject2Groups: TList;
FObject2Items: TList;
protected
procedure FreeObjects;
public
constructor Create(AName: string);
destructor Destroy; override;
procedure Save(FileName: string);
function Load(Filename: string): Boolean;
published
property Name: string read FName write FName;
property Object1Groups: TList read FObject1Groups;
property Object1Items: TList read FObject1Items;
property Object2Groups: TList read FObject2Groups;
property Object2Items: TList read FObject2Items;
end;
When I save the Main Object to XML I first iterate the whole TreeView and then assign to each Object the Node data such as Parent, Level, Index etc. The output XML File based on the first image would look like this:
Note: The SomeVal parts are not important as I never bothered writing anything to the Objects.
Really what I should do is Save to the XML just as the Treeview is represented. I am not too familar with XML as I am still getting to grips with it, but I think the output should look something like this: (written in Notepad)
<XML Name="test.xml">
<Counts Object1Groups="3" Object1Items="5" Object2Groups="2" Object2Items="1" />
<TObject1RootGroup Name="Object1Root" Group="" NodeLevel="0" NodeIndex="0"
<TObject1Item Name="Item1" Group="Object1Root" NodeLevel="1" NodeIndex="0" SomeVal1="" SomeVal2="" />
<TObject1Item Name="Item2" Group="Object1Root" NodeLevel="1" NodeIndex="1" SomeVal1="" SomeVal2="" />
<TObject1Group Name="Group1" Group="Object1Root" NodeLevel="1" NodeIndex="2" />
<TObject1Item Name="Item3" Group="Object1Root" NodeLevel="1" NodeIndex="3" SomeVal1="" SomeVal2="" />
<TObject1Group Name="Group2" Group="Object1Root" NodeLevel="1" NodeIndex="4" />
<TObject1Item Name="Item1" Group="Group2" NodeLevel="2" NodeIndex="0" SomeVal1="" SomeVal2="" />
<TObject1Group Name="Group1" Group="Group2" NodeLevel="2" NodeIndex="1" />
<TObject1Item Name="Item1" Group="Group1" NodeLevel="3" NodeIndex="0" SomeVal1="" SomeVal2="" />
<TObject2RootGroup Name="Object2Root" Group="" NodeLevel="0" NodeIndex="1"
<TObject2Group Name="Group1" Group="Object2Root" NodeLevel="1" NodeIndex="0" />
<TObject2Group Name="Group2" Group="Object2Root" NodeLevel="1" NodeIndex="1" />
<TObject2Item Name="Item1" Group="Group2" NodeLevel="2" NodeIndex="0" SomeVal1="" SomeVal2="" />
</XML>
Then I could load the TreeView from the XML. The problem is I only really know how to save the XML as I currently am, I know some kind of recursion etc is needed and this is where I would struggle, and particularly rebuilding the Tree from the XML File.
Attachment
It has taken me a few hours to strip down my actual project code into an example that is easier to read and understand, it is written in Lazarus and uses the OmniXML library, I have only included the source units no project file.
Download it here (the password is stackoverflow): http://www34.zippyshare.com/v/16401041/file.html
Ultimately my question is:
- How to save to XML with correct hierarchy structure.
- How to load the XML and rebuild the Treeview to exactly how it was before save.
Many thanks.