Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, xmldom, xmlintf, msxmldom, xmldoc, stdctrls; Type tform1 = Class (tform) xmldocument1: txmldocument; button1: tbutton; button2: tbutton; Procedure upload (Sender: tobject); Procedure button1click (Sender: tobject); Procedure button2click (Sender: tobject ); private {private Declarations} public {public declarations} end; var form1: tform1; implementation {$ R *. DFM} // open procedure tform1.formcreate (Sender: tobject); begin xmldocument1.loadfromfile ('C: \ temp \ test. XML '); {the same return value can be obtained only when an XML test file is provided.} end; // haschildnodes: determines whether the child node procedure tform1.button1click (Sender: tobject) is included ); vaR node: ixmlnode; B: Boolean; begin {we take a node from the example, such as the first node under the root node: <personnel position = "section chief" Remarks = "bureau level">
<Name> Zhang San </Name>
<Gender> male </gender>
<Age> 34 </age>
</Personnel >}{ obtain the node with the Node object} node: = xmldocument1.documentelement. childnodes [0]; {of course, this node contains sub-nodes and contains three sub-nodes} B: = node. haschildnodes; showmessage (booltostr (B); {-1; that is, true} showmessage (inttostr (node. childnodes. count); {3} showmessage (node. nodename); {personnel} {Look at the "name" node: <Name> JOHN </Name>
} {Get this node with the Node object} node: = xmldocument1.documentelement. childnodes [0]. childnodes [0]; B: = node. haschildnodes; showmessage (booltostr (B); {-1; that is, true} showmessage (inttostr (node. childnodes. count); {1} showmessage (node. nodename); {name} {it only contains the text "Zhang San", there is no label, then the node has the text attribute} showmessage (node. text); {Zhang San} {get the text node "Zhang San"} node: = xmldocument1.documentelement. childnodes [0]. childnodes [0]. childnodes [0]; B: = node. haschildnodes; showmessage (booltostr (B); {0; that is, false no subnode} showmessage (inttostr (node. childnodes. count); {0; Total number of subnodes is 0} showmessage (node. nodename); {# text; indicates that it is a text node} showmessage (node. nodevalue); {Zhang San; we can also retrieve its value} {the nodevalue attribute of the text node is equivalent to the text attribute of the upper node} showmessage (node. parentnode. text); {zhangsan} end; // istextelement: determines whether the procedure tform1.button2click (Sender: tobject); var node: ixmlnode; B: Boolean; begin {Let's take out a node from the example, for example, the first node under the root node: <personnel position = "section chief" Remarks = "bureau level">
<Name> Zhang San </Name>
<Gender> male </gender>
<Age> 34 </age>
</Personnel >}{ obtain the node with the Node object} node: = xmldocument1.documentelement. childnodes [0]; {this node certainly has no text attribute} B: = node. istextelement; showmessage (booltostr (B); {0; that is, false} showmessage (node. nodename); {personnel} {"name" Node} node: = xmldocument1.documentelement. childnodes [0]. childnodes [0]; {"name" node has the text attribute} B: = node. istextelement; showmessage (booltostr (B); {-1; that is, true} showmessage (node. nodename); {name} {"zhangsan" text node} node: = xmldocument1.documentelement. childnodes [0]. childnodes [0]. childnodes [0]; {the text node does not have the text attribute} B: = node. istextelement; showmessage (booltostr (B); {0; that is, false} showmessage (node. nodename); {# text} end; end.