Re: parsing xml




"hgeron" <hgeron@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:7D1A41AC-E14C-40A9-B06E-A85B2DB7EC52@xxxxxxxxxxxxxxxx
Mark.
The for ... next on the call to Recurse didn't work at all.
I traced the code and after detailed listing of the first node,
it steps to "Catolog" which contains the balance of the 12 books xml file,
the program returns to the point following the function call and ends.
It does not detail the catolog as needed.
Does the LEVEL need to be changed so it continues to the following node?
=================================================
Sub mark()
Dim xmlDoc As New MSXML2.DOMDocument50
Dim Node As IXMLDOMNode
Dim NodeList As IXMLDOMNodeList
xmlDoc.Load ("m:\xml\books.xml")
RecurseXML xmlDoc, 0
End Sub
Function RecurseXML(Node As IXMLDOMNode, Level As Long) As Boolean
Dim NodeList As IXMLDOMNodeList
MsgBox ("Begin Recurse Level: " & Level)
Set NodeList = Node.childNodes
Dim Att As IXMLDOMAttribute

This test at this point in the code is a logic flaw:

If Not Node.Attributes Is Nothing Then

This code reads, "if the attributes collection isn't nothing, display each
attribute, *otherwise* recurse each child node." Attributes will be nothing
if the node has a data childnode, but typically it will be a 0-or-more item
collection, in which case (as it was) no recursive call was made.

Also, trying to enumerate a NODE_TEXT node throws an error... so try this:

---------------------------

Sub mark()
Dim xmlDoc As New MSXML2.DOMDocument50
Dim Node As IXMLDOMNode
Dim NodeList As IXMLDOMNodeList
xmlDoc.Load ("m:\xml\books.xml")

RecurseXML xmlDoc.documentElement, 0

End Sub

Function RecurseXML(Node As IXMLDOMNode, Level As Long) As Boolean
Dim NodeList As IXMLDOMNodeList
Dim ChildNode As IXMLDOMNode
MsgBox ("Begin Recurse Level: " & Level)
Set NodeList = Node.childNodes
Dim Att As IXMLDOMAttribute
If Not Node.Attributes Is Nothing Then
For Each Att In Node.Attributes
Debug.Print ("ATTR:" & vbCr & Att.Value)
Next
End If

If Node.nodeType = NODE_TEXT Then
Debug.Print "NODE_TEXT:" & vbCr & Node.xml
Else
If Not NodeList Is Nothing Then
For Each ChildNode In NodeList
Debug.Print ("NODE: " & vbCr & Node.xml)
If RecurseXML(ChildNode, Level + 1) = False Then Exit
Function
Next
End If
End If
RecurseXML = True
End Function

---------------------------

I changed most of the msgboxes to Debug.Print statements, and it still makes
you click Ok until your fingers bleed. I actually tested it with
books.xml -- which is not to say it will work with all xml in the world, by
any stretch, but at least it should end this thread.

Note that when tracing into code to debug it, it is useful to trace into the
function that isn't working as expected. That will usually make it apparent
where the logic is inadequate. Step through line by line, or set break
points in key places. When a condition doesn't work as expected, test it in
the immediate window, check all values involved...

It's what we call "debugging", there is no substitute. In some ways I have
done you a disservice by handing this to you, you'd have learned more by
figuring it out yourself... Oh well, c'est la vie. :-)


-Mark




If Not Node.Attributes Is Nothing Then
For Each Att In Node.Attributes
If MsgBox("ATTR:" & vbCr & Att.Value, vbOKCancel) =
vbCancel
Then Exit Function
Next
Else
For Each Node In NodeList
If MsgBox("NODE: " & vbCr & Node.XML, vbOKCancel) = vbCancel
Then Exit Function
If RecurseXML(Node, Level + 1) = False Then Exit Function
Next
End If
RecurseXML = True
End Function




.



Relevant Pages

  • Re: parsing xml
    ... Dim xmlDoc As New MSXML2.DOMDocument50 ... Function RecurseXML(_ ... Private Sub Command1_Click ... Dim Node As IXMLDOMNode ...
    (microsoft.public.data.ado)
  • Re: parsing xml
    ... point on the Exit Function lines, ... Dim xmlDoc As New MSXML2.DOMDocument50 ... RecurseXML xmlDoc, 0 ... Function RecurseXML(Node As IXMLDOMNode, ...
    (microsoft.public.data.ado)
  • Re: parsing xml
    ... I got a "fix" that seems to work. ... The extra size is repeating nodes. ... Dim xmlDoc As New MSXML2.DOMDocument50 ... Function RecurseXML(Node As IXMLDOMNode, ...
    (microsoft.public.data.ado)
  • Re: parsing xml
    ... The extra size is repeating nodes. ... Dim xmlDoc As New MSXML2.DOMDocument50 ... Function RecurseXML(Node As IXMLDOMNode, ... attnum = attnum + 1 ...
    (microsoft.public.data.ado)
  • Re: parsing xml
    ... "Mark J. McGinty" wrote: ... The extra size is repeating nodes. ... Dim xmlDoc As New MSXML2.DOMDocument50 ... Function RecurseXML(Node As IXMLDOMNode, ...
    (microsoft.public.data.ado)