In Delphi, The txmldocument component is used to read XML to generate the Treeview function memo.

Source: Internet
Author: User

1. Save the Treeview structure as an XML file Function

Procedure tmainform. tree2xml (tree: ttreeview );

VaR

TN: ttreenode;

Xmldoc: txmldocument;

Inode: ixmlnode;

 

Procedure processtreeitem (TN: ttreenode; inode: ixmlnode );

VaR

CNode: ixmlnode;

Begin

If Tn = nil then exit;

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

CNode. attributes ['text']: = tn. text;

CNode. attributes ['imageindex']: = tn. imageindex;

CNode. attributes ['selectedindex ']: = tn. selectedindex;

 

// Child Nodes

TN: = tn. getfirstchild;

Whiletn <> 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. encoding: = 'utf-8 ';

Xmldoc. Version: = '1. 0 ';

 

Xmldoc. savetofile (changefileext (paramstr (0), '. xml '));

Xmldoc: = nil;

End;

 

Ii. Generate a Treeview from XML

 

Procedure tmainform. xml2tree (tree: ttreeview; xmldoc: txmldocument );

VaR

Inode: ixmlnode;

 

Procedure processnode (node: ixmlnode; TN: ttreenode );

VaR

CNode: ixmlnode;

Begin

If node = nil then exit;

With node do

Begin

TN: = tree. Items. addchild (TN, attributes ['text']);

Tn. imageindex: = INTEGER (attributes ['imageindex']);

Tn. selectedindex: = INTEGER (attributes ['selectedindex ']);

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.