Unit unit1;
Interface
Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, xmldom, xmlintf, msxmldom, xmldoc, stdctrls, extctrls, comctrls;
Type
Tform1 = Class (tform)
Treeview1: ttreeview;
Panel1: tpanel;
Btnload: tbutton;
Button1: tbutton;
Button2: tbutton;
Button3: tbutton;
Xmldocument1: txmldocument;
Opendialog1: topendialog;
Procedure btnloadclick (Sender: tobject );
Procedure button1click (Sender: tobject );
Procedure button2click (Sender: tobject );
Procedure button3click (Sender: tobject );
Private
Procedure domtotree (xmlnode: ixmlnode; treenode: ttreenode );
{Private Declarations}
Public
{Public declarations}
End;
VaR
Form1: tform1;
Implementation
{$ R *. DFM}
Procedure tform1.btnloadclick (Sender: tobject );
// Import the XML file
Begin
Opendialog1.initialdir: = extractfilepath (application. exename );
If opendialog1.execute then
Begin
Xmldocument1.loadfromfile (opendialog1.filename );
Treeview1.items. Clear;
Domtotree (xmldocument1.documentelement, nil );
Treeview1.fullexpand;
End;
End;
Procedure tform1.button1click (Sender: tobject );
Begin
Showmessage (xmldocument1.documentelement.
Attributes ['text']);
End;
Procedure tform1.button2click (Sender: tobject );
Begin
Showmessage (xmldocument1.documentelement.
Childnodes. nodes [1]. childvalues ['author']);
End;
Procedure tform1.button3click (Sender: tobject );
Begin
// Display information
Showmessage (xmldocument1.documentelement. attributes ['test']);
End;
Procedure tform1.domtotree (xmlnode: ixmlnode; treenode: ttreenode );
VaR
I: integer;
Newtreenode: ttreenode;
Nodetext: string;
Attrnode: ixmlnode;
Begin
// Exit the program if no node exists.
If not (xmlnode. nodetype = ntelement) then
Exit;
// Add a node
Nodetext: = xmlnode. nodename;
If xmlnode. istextelement then
Nodetext: = nodetext + '=' + xmlnode. nodevalue;
Newtreenode: = treeview1.items. addchild (treenode, nodetext );
// Add attributes
For I: = 0 to xmlnode. attributenodes. Count-1 do
Begin
Attrnode: = xmlnode. attributenodes. nodes [I];
Treeview1.items. addchild (newtreenode,
'[' + Attrnode. nodename + '= "' + attrnode. Text + '"]');
End;
// Add a subnode
If xmlnode. haschildnodes then
For I: = 0 to xmlnode. childnodes. Count-1 do
Domtotree (xmlnode. childnodes. nodes [I], newtreenode );
End;
End.