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&nbsp;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&nbsp;a recursive function?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Any help would be =
appreciated.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Code:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</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&nbsp; =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&nbsp;&nbsp; =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>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>oBase =3D =
oXmldoc.DocumentElement<BR>If=20
!oBase.hasChildNodes<BR>&nbsp;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>&nbsp;getnode(oTree,=20
oNodes, cParent, @iNodekey, @cNodekey, 1, @c1, @l1)<BR>&nbsp;If l1 And=20
oChild1.hasChildNodes<BR>&nbsp;&nbsp;For Each oChild2 In=20
oChild1.childnodes<BR>&nbsp;&nbsp;&nbsp;getnode(oTree, oNodes, cNodekey, =

@iNodekey, @ccNodekey, 2, @c2, @l2)<BR>&nbsp;&nbsp;&nbsp;If l2 And=20
oChild2.hasChildNodes<BR>&nbsp;&nbsp;&nbsp;&nbsp;For Each oChild3 In=20
oChild2.childnodes<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getnode(oTree, =
oNodes,=20
ccNodekey, @iNodekey, @cccNodekey, 3, @c3,=20
@l3)<BR>&nbsp;&nbsp;&nbsp;&nbsp;Next<BR>&nbsp;&nbsp;&nbsp;Endif<BR>&nbsp;=
&nbsp;Next<BR>&nbsp;Endif<BR>Next</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV><FONT =
face=3D"Courier New" size=3D2>
<DIV><FONT face=3DArial></FONT><FONT face=3DArial></FONT><BR>Function=20
getnode<BR>&nbsp;Parameters oTv, oNd, cPr, iKey, cKey, nLevel, lc, =
ll</DIV>
<DIV><FONT face=3DArial></FONT>&nbsp;</DIV>
<DIV>&nbsp;iKey =3D iKey + 1<BR>&nbsp;cKey =3D Replicate("c", nLevel) +=20
Alltrim(Str(iKey))<BR>&nbsp;cChild =3D 'oChild' + =
Transform(nLevel)<BR>&nbsp;If=20
&amp;cChild..nodename &lt;&gt; lc<BR>&nbsp;&nbsp;oNd.Add(cPr, 4, cKey,=20
&amp;cChild..nodename)<BR>&nbsp;&nbsp;oTv.nodes(cKey).Tag =3D=20
&amp;cChild..Text<BR>&nbsp;&nbsp;lc =3D=20
&amp;cChild..nodename<BR>&nbsp;Else<BR>&nbsp;&nbsp;ll =3D=20
.F.<BR>&nbsp;Endif<BR>return</FONT></DIV></BODY></HTML>

------=_NextPart_000_000A_01C736F9.07B138C0--

Re: Generic XML reader by Paul

Paul
Tue Jan 16 04:26:33 CST 2007

This is a multi-part message in MIME format.

------=_NextPart_000_000D_01C739B4.FEC30A40
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Yes, I would definitely use a recursive function.
"Krister" <nospam@myprovider.com> wrote in message =
news:x91qh.29023$E02.11979@newsb.telia.net...
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_000D_01C739B4.FEC30A40
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 bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Yes, I would definitely use a recursive =

function.</FONT></DIV>
<BLOCKQUOTE dir=3Dltr=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV>"Krister" &lt;<A=20
href=3D"mailto:nospam@myprovider.com">nospam@myprovider.com</A>&gt; =
wrote in=20
message <A=20
=
href=3D"news:x91qh.29023$E02.11979@newsb.telia.net">news:x91qh.29023$E02.=
11979@newsb.telia.net</A>...</DIV>
<DIV><FONT face=3DArial size=3D2>The code&nbsp;below works for reading =
up to 3=20
levels of an XML file with into a treeview control. Now I would like =
to=20
develop this code to make it generic and so it takes *any* number of =
levels.=20
How can I do this - would I need to use&nbsp;a recursive=20
function?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Any help would be =
appreciated.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Code:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</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&nbsp; =3D=20
1<BR>oXmldoc =3D Createobject('msxml2.domdocument')<BR>oXmldoc.Async =
=3D=20
.F.<BR>cXMLDoc=3D Locfile('*.xml',=20
'xml')<BR>oXmldoc.Load(cXMLDoc)<BR>oTree&nbsp;&nbsp; =3D=20
oForm.treeview1<BR>oNodes =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>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>oBase =3D =
oXmldoc.DocumentElement<BR>If=20
!oBase.hasChildNodes<BR>&nbsp;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>&nbsp;getnode(oTree,=20
oNodes, cParent, @iNodekey, @cNodekey, 1, @c1, @l1)<BR>&nbsp;If l1 And =

oChild1.hasChildNodes<BR>&nbsp;&nbsp;For Each oChild2 In=20
oChild1.childnodes<BR>&nbsp;&nbsp;&nbsp;getnode(oTree, oNodes, =
cNodekey,=20
@iNodekey, @ccNodekey, 2, @c2, @l2)<BR>&nbsp;&nbsp;&nbsp;If l2 And=20
oChild2.hasChildNodes<BR>&nbsp;&nbsp;&nbsp;&nbsp;For Each oChild3 In=20
oChild2.childnodes<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getnode(oTree, =
oNodes,=20
ccNodekey, @iNodekey, @cccNodekey, 3, @c3,=20
=
@l3)<BR>&nbsp;&nbsp;&nbsp;&nbsp;Next<BR>&nbsp;&nbsp;&nbsp;Endif<BR>&nbsp;=
&nbsp;Next<BR>&nbsp;Endif<BR>Next</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV><FONT =
face=3D"Courier New"=20
size=3D2>
<DIV><FONT face=3DArial></FONT><FONT face=3DArial></FONT><BR>Function=20
getnode<BR>&nbsp;Parameters oTv, oNd, cPr, iKey, cKey, nLevel, lc, =
ll</DIV>
<DIV><FONT face=3DArial></FONT>&nbsp;</DIV>
<DIV>&nbsp;iKey =3D iKey + 1<BR>&nbsp;cKey =3D Replicate("c", nLevel) =
+=20
Alltrim(Str(iKey))<BR>&nbsp;cChild =3D 'oChild' + =
Transform(nLevel)<BR>&nbsp;If=20
&amp;cChild..nodename &lt;&gt; lc<BR>&nbsp;&nbsp;oNd.Add(cPr, 4, cKey, =

&amp;cChild..nodename)<BR>&nbsp;&nbsp;oTv.nodes(cKey).Tag =3D=20
&amp;cChild..Text<BR>&nbsp;&nbsp;lc =3D=20
&amp;cChild..nodename<BR>&nbsp;Else<BR>&nbsp;&nbsp;ll =3D=20
.F.<BR>&nbsp;Endif<BR>return</FONT></DIV></BLOCKQUOTE></BODY></HTML>

------=_NextPart_000_000D_01C739B4.FEC30A40--