473,379 Members | 1,191 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Walking the DOM with VB6

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
2 9695
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: evski | last post by:
kia ora I am after a set of php script to administer bookings for buses and huts and coordinate customer enquiries in our small tourism business I have looked extensively but not come up with...
0
by: Chris Lyon | last post by:
I handle a lot of audio files of different formats, which live in a directory tree. WAV files in a wav directory, AIFF's in an AIFF directory next to the wav directory and mp3's in an MP3 directory...
2
by: mr.happy | last post by:
Hi all, I have this little question, basicly i solved it already by writing a little bit of code for it (using recursion), but still i am wondering if there is a shorter ways to do things (like...
4
by: Jim Bancroft | last post by:
Sorry for the basic nature of this question. I know XSL can do this, but I don't recall a good method... Say I have an xml structure like this: <folder_structure> <folder name="folder1">...
1
by: Zachary Hartnett | last post by:
I was trying to write a routine this morning that would open a given assembly, walk the inheritance tree of classes in the assembly, and provide a list of classes in the assembly that inherit from...
5
by: TrulyUnusualdotcom | last post by:
I'm reading PHP & MySQL for Dummies 2nd edition...ya ya I know..lame. Anyway I got to the part about walking through an array and I just can't seem to figure out what this would be used for. What...
8
by: Ben Hallert | last post by:
Hi guys! I'm working on a little javascriptlet/greasemonkey script, and I've run into a challenge that I think can be solved with walking the DOM, but I feel like I'm kludging my way through and...
11
by: Simon Woods | last post by:
Hi I have this recursive function and I want to walk the inheritance hierarchy to set field values .... the generic T is constrainted as the base class of the inheritance hierarchy Friend...
3
by: Backiyaraj | last post by:
which is the correct one 'he came to school by walk' or 'he came to school walking'
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.