Export TreeView to XML. Load TreeView from XML

xiaoxiao2021-03-06  113

Export TreeView to XML. Load TreeView from XML

Export TreeView to XML. Load TreeView from XML

This article guides how to save the TtreeView component content (the text and other properties of the tree node) in XML, and guide how to restore TreeView from the XML file.

The Delphi TtreeView component (can be found in the "Win32" component panel) to display a hierarchical item window. Each item contains a tag and an optional bitmap (two more accurate) .xml make developers Can better use structural data. The structure of the XML document is very similar to TreeView's structure. Delphi, TXMLDocument (can "Internet" component panel) can be used to represent an XML document. This article will teach you how to save the tree directory XML file and how to rebuild a tree directory from an XML document. If you need a powerful, easy-to-transfer format to save the application configuration, use TreeView and XML a very good choice, then see ..... Save TtreeView items to XML First, we create a simple application and place several components: a TTREEVIEW component, two imagelist components, and a TXMLDocument component. One of the images are used to associate the Image of TreeView, A STATEIMAGES attribute (please refer to: How to add a check box and radio button to the TreeView). This is a screenshot of the program runtime. The Tree2XML process is used to save the tree model to an XML document. It has only one parameter, Used to give a TTREEVIEW component to be saved to XML. XML document uses the same file name as the application (of course, you want to use '.xml' as the extension) This process traverses the entire TreeView, and each time you call the sub-process Processtreeitm. Process ProcessTreeItem recursively calls in the same level node. This recursive algorithm ensures that the entire TreeView node is processed. For each directory entry, an XML node is established and has an attribute value indicating the text and ImageIndex, StateIndex properties. The node actually saves all values ​​related to the tree node. Once all tree records are processed, the XML file is also saved. Note: Process ProcessTreeItem is dynamically created and operated with the TXMLDocument (XMLDoc) component.

Procedure Tree2XML (Tree: TtreeView);

VAR

TTREENODE;

Xmldoc: txmldocument;

IXMLNODE;

Procedure Processtreeit

TTREENODE;

Inode: ixmlnode;

VAR

IXMLNode;

Begin

IF (tn = nil).

Cnode: = inode.addchild ('item');

Cnode.attributes ['Text']: = Tn.Text;

Cnode.attributes ['ImageIndex']: = tn.imageIndex;

Cnode.attributes ['StateIndex']: = TN.StateIndex;

// child node

TN: = Tn.GetFirstChild;

While TN <> NIL DO

Begin

ProcesstreeItem (TN, CNODE);

TN: = tn.getnextsibling;

END;

End; (* ProcesstreeItem *)

Begin

XMLDoc: = txmldocument.create (nil);

XMLDoc.Active: = TRUE;

Inode: = xmldoc.addchild ('Tree2XML'); inode.attributes ['app']: = paramstr (0);

TN: = Tree.topItem;

While TN <> NIL DO

Begin

ProcesstreeItem (TN, Inode);

TN: = tn.getnextsibling;

END;

XMLDoc.savetofile (ChangefileExt (paramstr (0), '. Xml'));

Xmldoc: = nil;

END; (* Tree2XML *)

In practical applications, you can export TtreeView to XML when the application is turned off, use the Form's OnCloseQuery event to ask the user to save:

// Tree is the name of the TTREEVIEW component

Procedure TFORM1.FORMCLOSEQUERY

Sender: TOBJECT;

Var canclose: boolean;

Begin

Case Messagedlg ('Save Tree Items to XML?',

MTConfirmation,

[mbyes, mbno, mbcancel], 0) of

MRYES:

Tree2XML (Tree);

MRNO:

CANCLOSE: = True;

Mrcancel:

CANCLOSE: = FALSE;

END;

END;

Below is the result of exporting the XML document.

Once we have an XML file that represents TreeView, we can use it to populate TreeView. When the application starts, the XML2Tree process is called to construct the tree directory. The Tree parameter represents the TtreeView component we have to populate; The XMLDoc parameter points to a TXMLDocument component.

Procedure XML2Tree

TTREEVIEW;

XMLDoc: txmldocument;

VAR

IXMLNODE;

Procedure processnode

IXMLNode;

TN: TTREENODE);

VAR

IXMLNode;

Begin

IF node = nil dam

With node do

Begin

TN: = Tree.Items.Addchild (TN, Attributes ['Text']);

TN.ImageIndex: = Integer (Attributes ['ImageIndex'];

TN.StateIndex: = Integer (Attributes ['StateIndex'];

END;

Cnode: = node.childnodes.first;

While Cnode <> NIL DO

Begin

ProcessNode (CNODE, TN);

CNODE: = cnode.nextsibling;

END;

End; (* processnode *)

Begin

Tree.Items.clear;

Xmldoc.FileName: = ChangefileExt (paramstr (0), '. Xml');

XMLDoc.Active: = TRUE;

Inode: = xmldoc.documentelement.childNodes.first;

While Inode <> NIL DO

Begin

ProcessNode (Inode, NIL);

Inode: = inode.nextsibling;

END;

XMLDoc.Active: = false;

END;

The XML2Tree process is basically similar to the Tree2XML operation principle. Code traverses all XML nodes, create a TTREENODE object and add to the TreeView component. This is the honesty, in the current project, I have rarely use the registry or INI Files to save configuration data. The XML seems to be more friendly. You can download the complete source code of this article. If you need help, please ask your questions in Delphi Programming Forum.

转载请注明原文地址:https://www.9cbs.com/read-102063.html

New Post(0)