Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Newbie to XML.

From: Peter Flynn <peter.nosp@-.--------.-->
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 ***


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent