The nodes in the XML document are standard tree structures, which can be recursive or backtrackingAlgorithmTo traverse all nodes.
This example uses recursive algorithms as an example to create two examples for Traversing XML documents.
In the first example, an XML node is provided to traverse all child nodes, excluding the Start Node.
The second example shows an XML node that traverses the given node and all its subnodes.
The preceding two examples show the tag attributes and text values of the XML document.Code.
<? XML version = "1.0" encoding = "gb2312"?>
<Root website = "http://www.why100000.com">
<Channel id = "1" tagname = "channel">
<Topic id = "1-1" tagname = "topic"> Windows channel </topic>
<URL id = "1-2" tagname = "url"> www.why00000.com/_windows </URL>
</Channel>
<Channel id = "2" tagname = "channel">
<Topic id = "2-1" tagname = "topic"> code lab </topic>
<Subweb id = "2-2" tagname = "subweb"> http://lab.why100000.com </subweb>
</Channel>
<BBS id = "03" tagname = "BBS">
<Topic id = "03-1" tagname = "topic"> Computer Learning Community </Topic>
<Subweb id = "03-2" tagname = "subweb"> http://bbs.why100000.com </subweb>
<Subbbs id = "03-3">
<Subbbstopic id = "03-3-1"> cainiao college </subbbstopic>
<Subbbstopic id = "03-3-2"> ask </subbbstopic>
<Subbbstopic id = "03-3-3"> learn it </subbbstopic>
<Subbbstopic id = "03-3-4"> back </subbbstopic>
<Subbbstopic id = "03-3-5"> </subbbstopic>
<Subbbstopic> </subbbstopic>
</Subbbs>
</BBS>
<Anathertag note = "testTag-1"/>
<Anathertag/>
</Root>
{----------------------------------------------------------------------}
Function tform1.getxmltree1 (nnode: ixmlnode): string;
VaR
I, J: integer;
Begin
For I: = 0 to nnode. childnodes. Count-1 do
Begin
S: = S + '<' + nnode. childnodes. nodes [I]. nodename;
For J: = 0 to nnode. childnodes [I]. attributenodes. Count-1 do
Begin
S: = S + ''+ nnode. childnodes [I]. attributenodes [J]. nodename;
S: = S + '= "' + nnode. childnodes [I]. attributenodes [J]. nodevalue + '"';
End;
S: = S + '> ';
If nnode. childnodes. nodes [I]. istextelement then
S: = S + nnode. childnodes. nodes [I]. text;
If nnode. haschildnodes and not nnode. childnodes. nodes [I]. istextelement then
Begin
S: = S + #13 + #10;
Getxmltree1 (nnode. childnodes. nodes [I]);
End;
S: = S + '</' + nnode. childnodes. nodes [I]. nodename + '>' + #13 + #10;
End;
Result: = s;
End;
{----------------------------------------------------------------------}
Function tform1.getxmltree2 (nnode: ixmlnode): string;
VaR
I, J: integer;
Begin
S: = S + '<' + nnode. nodename;
For J: = 0 to nnode. attributenodes. Count-1 do
Begin
S: = S + ''+ nnode. attributenodes [J]. nodename;
S: = S + '= "' + nnode. attributenodes [J]. nodevalue + '"';
End;
S: = S + '> ';
If nnode. istextelement then
S: = S + nnode. Text
Else
Begin
S: = S + #13 + #10;
If nnode. haschildnodes then
For I: = 0 to nnode. childnodes. Count-1 do
Begin
Getxmltree2 (nnode. childnodes. nodes [I]);
End;
End;
S: = S + '</' + nnode. nodename + '>' + #13 + #10;
Result: = s;
End;
{----------------------------------------------------------------------}
Call:
procedure tform1.button4click (Sender: tobject);
var
oxml: txmldocument;
begin
oxml: = txmldocument. create (Self);
oxml. filename: = '_ Treeview. xml';
oxml. active: = true;
S: = '';
S: = getxmltree1 (oxml. childnodes. findnode ('root');
memo1.lines. add (s);
oxml. free;
end;
Procedure tform1.button5click (Sender: tobject );
VaR
Oxml: txmldocument;
Begin
Oxml: = txmldocument. Create (Self );
Oxml. filename: = '_ Treeview. xml ';
Oxml. Active: = true;
S: = '';
S: = getxmltree2 (oxml. childnodes. findnode ('root '));
Memo1.lines. Add (s );
Oxml. Free;
End;
"100,000 why" original documents of Computer Learning Network
Reprinted please indicate the source: http://www.why100000.com