Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Newbie to XML. >Thread Next - Re: Newbie to XML. Re: Newbie to XML.To: NULL Date: 10/5/2006 12:24:00 AM
Anand Kasotia wrote:
> I am using XML DOM in VB6 to traverse through the XML file sequentially
> (Top to Bottom) to grab all the tags.
Do you need to record how deep you found each element type,
or in what context (parentage) it occurred; or do you just
want to list the unique occurrences of the names you found?
> Can someone expand on the code I have written below. My code only goes
> two level deep in the XML "Tree structure". WITHOUT knowing how may
> levels there are in the XML file, how can I traverse through and grab
> all the tags.
If you just want a list of element type names and how many
there were, without anything about *where* they were, this
command will do it (you'll need the Unix utilities for Windows
from Cygwin or similar):
cat myfile.xml | tr '\012\015<>' '\040\040\012\040' | grep -Ev
'^(\?|!|/|$)' | awk '{print $1}' | sort | uniq -c | sort -k 1nr
If you need to retain the positional information then you
need to use recursion. That way you don't need to know how
many branches are in the tree or where they are: it'll just
keep going down until it runs out of depth, then start on
the next branch. In processing XML, think declaratively:
recursion is much more important than looping, which is a
procedural activity.
///Peter
--
XML FAQ: http://xml.silmaril.ie/
> I know selectNodes("//*") will accomplish this but want to know
> programmatically how to read through the entire XML file and grabbing
> all the XML tags without knowing how many level deep the XML files goes.
>
> Private Sub Command2_Click()
> Dim lFile_OutA As Long
> Dim sTempVarA As String
> Dim xmlDoc As New MSXML2.DOMDocument30
> xmlDoc.async = False
> xmlDoc.Load "C:\71991bw0000000Out.XML"
>
> sTempVarA = sTempVarA & xmlDoc.nodeName & vbCrLf
>
> Dim x As IXMLDOMElement
> Set x = xmlDoc.documentElement
> sTempVarA = sTempVarA & x.nodeName & vbCrLf
>
> Dim y As IXMLDOMNodeList
> Set y = x.childNodes
>
> For i = 0 To y.length - 1
> sTempVarA = sTempVarA & y(i).nodeName & vbCrLf
> For z = 0 To y(i).childNodes.length - 1
> If y(i).hasChildNodes = True And
> y(i).childNodes(z).nodeType <> 3 Then
> sTempVarA = sTempVarA & y(i).childNodes(z).nodeName
> & vbCrLf
> End If
> Next
> Next
> lFile_OutA = FreeFile
> Open "C:\output.txt" For Output As lFile_OutA
> Print #lFile_OutA, sTempVarA
> Close lFile_OutA
> End Sub
>
> Thanks.
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
