By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,771 Members | 1,741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,771 IT Pros & Developers. It's quick & easy.

Walking the DOM with VB6

P: n/a
Okay, this may be a bit of a newbie question but I could really use some
help.

I want to write a set of routines that will read in the contents of XML
files. I'll be reading the tag names, attributes and data into other
interfaces, stuffing arrays, loading trees, etc.

I will not know the structure of these files ahead of time. Some values may
have attributes, some may or may not have children and I will not know how
many deep these tags pairs may go.

I can start walking the DOM and check at each level how many siblings are
there, if any tags have attributes and if they have children etc but how do
I next these tests when I don't know how many levels deep I'll need to go?

Here is my ruff and inadequate start:

Dim xmlDoc As New DOMDocument
Dim oNode As IXMLDOMNode
Dim oNodeList As IXMLDOMNodeList
Dim lCnt As Long
Dim lNodeListLen As Long
Dim bHasChildren As Boolean

xmlDoc.async = False
xmlDoc.validateOnParse = True
xmlDoc.resolveExternals = True
xmlDoc.Load (App.Path & "\MyFile.xml")

bHasChildren = False

Set oNode = xmlDoc.firstChild
PrintNodeInfo oNode 'This is just a routine to print out the values
' I could of course be loading the
values into other things

Set oNodeList = xmlDoc.childNodes

lNodeListLen = oNodeList.length

For lCnt = 0 To lNodeListLen - 1
PrintNodeInfo oNode
bHasChildren = oNodeList.Item(lCnt).hasChildNodes

If bHasChildren = True Then
MsgBox "Has kids!" ' From here I would need to dig deeper
and keep evaluating
Else
MsgBox "No Kids!"
End If

Next lCnt

I can't use counters this way because I would not know how many different
counters I'll need to go the number of levels deep I need to go.

There must be some other way to do this.

Thanks in advance for assistance.

-John
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
You would probably have to construct a do while loop. Not real good
on XML, so someone else here might give a better answer. I would read
the XML file as a text file, and rebuild the structure in arrays,
getting the info you want.

What do you want to get out of this - that will decide the best way to
go about decoding the XML file.

BTW VB.Net has the ability to read and decode the XML files for you,
just a thought

On Fri, 13 Aug 2004 03:50:13 GMT, "Who Cares" <me@my.net> wrote:
Okay, this may be a bit of a newbie question but I could really use some
help.

I want to write a set of routines that will read in the contents of XML
files. I'll be reading the tag names, attributes and data into other
interfaces, stuffing arrays, loading trees, etc.

I will not know the structure of these files ahead of time. Some values may
have attributes, some may or may not have children and I will not know how
many deep these tags pairs may go.

I can start walking the DOM and check at each level how many siblings are
there, if any tags have attributes and if they have children etc but how do
I next these tests when I don't know how many levels deep I'll need to go?

Here is my ruff and inadequate start:

Dim xmlDoc As New DOMDocument
Dim oNode As IXMLDOMNode
Dim oNodeList As IXMLDOMNodeList
Dim lCnt As Long
Dim lNodeListLen As Long
Dim bHasChildren As Boolean

xmlDoc.async = False
xmlDoc.validateOnParse = True
xmlDoc.resolveExternals = True
xmlDoc.Load (App.Path & "\MyFile.xml")

bHasChildren = False

Set oNode = xmlDoc.firstChild
PrintNodeInfo oNode 'This is just a routine to print out the values
' I could of course be loading the
values into other things

Set oNodeList = xmlDoc.childNodes

lNodeListLen = oNodeList.length

For lCnt = 0 To lNodeListLen - 1
PrintNodeInfo oNode
bHasChildren = oNodeList.Item(lCnt).hasChildNodes

If bHasChildren = True Then
MsgBox "Has kids!" ' From here I would need to dig deeper
and keep evaluating
Else
MsgBox "No Kids!"
End If

Next lCnt

I can't use counters this way because I would not know how many different
counters I'll need to go the number of levels deep I need to go.

There must be some other way to do this.

Thanks in advance for assistance.

-John


Jul 17 '05 #2

P: n/a
Eddie,

Thanks for the reply. Actually I'm sure I don't want to just parse a
string. I can't use VB.Net in this case because the Framework is just to
big <grin>.

Actually I've gotten closer. I just want to process unlimited depths of XML
nodes, get all the tag names, any attributes and data within the tags.
Since I've gotten closer I'll post my code.

Again, I appreciate any assistance. Please be kind to the newbie <smile>.

-John
Option Explicit

Dim bHasKids As Boolean
Dim bSuccess As Boolean
Dim oNodeList() As IXMLDOMNodeList
Dim oNodeListPointer As IXMLDOMNodeList
Dim oNode() As IXMLDOMNode

Private Sub Form_Load()
Dim xmlDoc As New DOMDocument
Dim root As IXMLDOMElement
Dim lCntNode As Long
Dim lCntList As Long

xmlDoc.async = False
xmlDoc.Load (App.Path & "\MyXMLFile.xml")

ReDim Preserve oNodeList(1)
ReDim Preserve oNode(1)

Set root = xmlDoc.documentElement
Set oNodeList(0) = root.childNodes

lCntNode = 0
lCntList = 0

For Each oNode(lCntNode) In oNodeList(lCntList)
bHasKids = GetNodeInfo(oNode(lCntNode))

If oNode(lCntNode).hasChildNodes = True Then

LowerNode lCntList, lCntNode

Set oNodeListPointer = oNode(lCntNode - 1).childNodes
bSuccess = ProcessSubNodes(oNodeListPointer)

RaiseNode lCntNode

End If

Next

Unload Form1
End
End Sub

Private Sub LowerNode(ByRef lCntList As Long, ByRef lCntNode As Long)

lCntList = lCntList + 1
lCntNode = lCntNode + 1
ReDim Preserve oNodeList(lCntList)

End Sub
Private Sub RaiseNode(ByRef lCntNode As Long)
lCntNode = lCntNode - 1
End Sub

Private Function ProcessSubNodes(ByVal oNodeList As IXMLDOMNodeList) As
Boolean
Dim i As Long
Dim lAttrib As Long
For i = 0 To oNodeList.length - 1
' bHasKids = GetNodeInfo(oNodeList.Item(i))

' Debug
lAttrib = oNodeList.Item(i).Attributes.length

If lAttrib >= 1 Then
Debug.Print " Attribute Found: "
& """" & _

oNodeList.Item(i).Attributes.Item(0).nodeValue & """"
End If

If oNodeList.Item(i).hasChildNodes = True Then
bHasKids = True
Else
bHasKids = False
End If
Next

End Function

Private Function GetNodeInfo(ByVal Item) As Boolean
Dim lAttrib As Long

Debug.Print "Base Name: " & """" & Item.baseName & """" & _
" Value: " & """" & Item.firstChild.nodeValue & """" &
_
" Has Child Nodes?: " & """" & Item.hasChildNodes &
""""
End Function


<Eddie B> wrote in message
news:d5********************************@4ax.com...
You would probably have to construct a do while loop. Not real good
on XML, so someone else here might give a better answer. I would read
the XML file as a text file, and rebuild the structure in arrays,
getting the info you want.

What do you want to get out of this - that will decide the best way to
go about decoding the XML file.

BTW VB.Net has the ability to read and decode the XML files for you,
just a thought

On Fri, 13 Aug 2004 03:50:13 GMT, "Who Cares" <me@my.net> wrote:
Okay, this may be a bit of a newbie question but I could really use some
help.

I want to write a set of routines that will read in the contents of XML
files. I'll be reading the tag names, attributes and data into other
interfaces, stuffing arrays, loading trees, etc.

I will not know the structure of these files ahead of time. Some values mayhave attributes, some may or may not have children and I will not know howmany deep these tags pairs may go.

I can start walking the DOM and check at each level how many siblings are
there, if any tags have attributes and if they have children etc but how doI next these tests when I don't know how many levels deep I'll need to go?
Here is my ruff and inadequate start:

Dim xmlDoc As New DOMDocument
Dim oNode As IXMLDOMNode
Dim oNodeList As IXMLDOMNodeList
Dim lCnt As Long
Dim lNodeListLen As Long
Dim bHasChildren As Boolean

xmlDoc.async = False
xmlDoc.validateOnParse = True
xmlDoc.resolveExternals = True
xmlDoc.Load (App.Path & "\MyFile.xml")

bHasChildren = False

Set oNode = xmlDoc.firstChild
PrintNodeInfo oNode 'This is just a routine to print out the values
' I could of course be loading the
values into other things

Set oNodeList = xmlDoc.childNodes

lNodeListLen = oNodeList.length

For lCnt = 0 To lNodeListLen - 1
PrintNodeInfo oNode
bHasChildren = oNodeList.Item(lCnt).hasChildNodes

If bHasChildren = True Then
MsgBox "Has kids!" ' From here I would need to dig deeper
and keep evaluating
Else
MsgBox "No Kids!"
End If

Next lCnt

I can't use counters this way because I would not know how many different
counters I'll need to go the number of levels deep I need to go.

There must be some other way to do this.

Thanks in advance for assistance.

-John

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.