This is a multi-part message in MIME format.
------=_NextPart_000_000A_01C736F9.07B138C0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
The code below works for reading up to 3 levels of an XML file with into =
a treeview control. Now I would like to develop this code to make it =
generic and so it takes *any* number of levels. How can I do this - =
would I need to use a recursive function?
Any help would be appreciated.
Code:
*** readxml.prg
Parameters oForm
iNodekey =3D 1
oXmldoc =3D Createobject('msxml2.domdocument')
oXmldoc.Async =3D .F.
cXMLDoc=3D Locfile('*.xml', 'xml')
oXmldoc.Load(cXMLDoc)
oTree =3D oForm.treeview1
oNodes =3D oTree.nodes
cParent =3D oXmldoc.DocumentElement.BaseName
oTree.nodes.Clear
oTree.nodes.Add(,, cParent, 'Rot: ' + cParent)
oBase =3D oXmldoc.DocumentElement
If !oBase.hasChildNodes
Return
Endif
Store ',|@' To c1, c2, c3
Store .T. To l1, l2, l3
Store '' To cNodekey, ccNodekey, cccNodekey
For Each oChild1 In oBase.childnodes
getnode(oTree, oNodes, cParent, @iNodekey, @cNodekey, 1, @c1, @l1)
If l1 And oChild1.hasChildNodes
For Each oChild2 In oChild1.childnodes
getnode(oTree, oNodes, cNodekey, @iNodekey, @ccNodekey, 2, @c2, @l2)
If l2 And oChild2.hasChildNodes
For Each oChild3 In oChild2.childnodes
getnode(oTree, oNodes, ccNodekey, @iNodekey, @cccNodekey, 3, @c3, =
@l3)
Next
Endif
Next
Endif
Next
Function getnode
Parameters oTv, oNd, cPr, iKey, cKey, nLevel, lc, ll
iKey =3D iKey + 1
cKey =3D Replicate("c", nLevel) + Alltrim(Str(iKey))
cChild =3D 'oChild' + Transform(nLevel)
If &cChild..nodename <> lc
oNd.Add(cPr, 4, cKey, &cChild..nodename)
oTv.nodes(cKey).Tag =3D &cChild..Text
lc =3D &cChild..nodename
Else
ll =3D .F.
Endif
return
------=_NextPart_000_000A_01C736F9.07B138C0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2900.3020" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2>The code below works for reading =
up to 3=20
levels of an XML file with into a treeview control. Now I would like to =
develop=20
this code to make it generic and so it takes *any* number of levels. How =
can I=20
do this - would I need to use a recursive function?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Any help would be =
appreciated.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Code:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2>*** readxml.prg</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>Parameters =
oForm<BR>iNodekey =3D=20
1<BR>oXmldoc =3D Createobject('msxml2.domdocument')<BR>oXmldoc.Async =3D =
.F.<BR>cXMLDoc=3D Locfile('*.xml',=20
'xml')<BR>oXmldoc.Load(cXMLDoc)<BR>oTree =3D =
oForm.treeview1<BR>oNodes=20
=3D oTree.nodes<BR>cParent =3D=20
oXmldoc.DocumentElement.BaseName<BR>oTree.nodes.Clear<BR>oTree.nodes.Add(=
,,=20
cParent, 'Rot: ' + cParent)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2>oBase =3D =
oXmldoc.DocumentElement<BR>If=20
!oBase.hasChildNodes<BR> Return<BR>Endif<BR>Store ',|@' To c1, c2,=20
c3<BR>Store .T. To l1, l2, l3<BR>Store '' To cNodekey, ccNodekey,=20
cccNodekey<BR>For Each oChild1 In =
oBase.childnodes<BR> getnode(oTree,=20
oNodes, cParent, @iNodekey, @cNodekey, 1, @c1, @l1)<BR> If l1 And=20
oChild1.hasChildNodes<BR> For Each oChild2 In=20
oChild1.childnodes<BR> getnode(oTree, oNodes, cNodekey, =
@iNodekey, @ccNodekey, 2, @c2, @l2)<BR> If l2 And=20
oChild2.hasChildNodes<BR> For Each oChild3 In=20
oChild2.childnodes<BR> getnode(oTree, =
oNodes,=20
ccNodekey, @iNodekey, @cccNodekey, 3, @c3,=20
@l3)<BR> Next<BR> Endif<BR> =
Next<BR> Endif<BR>Next</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV><FONT =
face=3D"Courier New" size=3D2>
<DIV><FONT face=3DArial></FONT><FONT face=3DArial></FONT><BR>Function=20
getnode<BR> Parameters oTv, oNd, cPr, iKey, cKey, nLevel, lc, =
ll</DIV>
<DIV><FONT face=3DArial></FONT> </DIV>
<DIV> iKey =3D iKey + 1<BR> cKey =3D Replicate("c", nLevel) +=20
Alltrim(Str(iKey))<BR> cChild =3D 'oChild' + =
Transform(nLevel)<BR> If=20
&cChild..nodename <> lc<BR> oNd.Add(cPr, 4, cKey,=20
&cChild..nodename)<BR> oTv.nodes(cKey).Tag =3D=20
&cChild..Text<BR> lc =3D=20
&cChild..nodename<BR> Else<BR> ll =3D=20
.F.<BR> Endif<BR>return</FONT></DIV></BODY></HTML>
------=_NextPart_000_000A_01C736F9.07B138C0--