msxml DOM: reading each element in an XML 
Author Message
 msxml DOM: reading each element in an XML

I am trying to read an XML (no other related files *.xsd...) with
MSXML DOM but I am trying to figure out how to navigate the file so
that I can access each element.

I am trying to read each element sequentially from top to bottom and
if there are elements with children <validation> I would want to read
those in as well.  When the children are complete it would go back to
the next sibling <site> after <validation>.  For some reason last week
I am almost sure I was doing it with the code listed but today when I
try it the <validation> sub-elements get skipped.

In code below I cut and pasted the code to show the essential
information.  I knew it was working because in my 'For Each' loop I
was reading in each element and setting the .text propery to a value
based on a simple text file that I would read 1 line of data which
contains only one value and put that value into the XML with the
currNode.Text = strValue.  Each tag from top to bottom was being
written to.

thanks for any help. David

XML:
<template>
        <validation>
                <request>T09</request>
                <per_number>2009</per_number>
        </validation>
        <site>-</site>
        <end_date>-</end_date>
</template>

vb6 code

Dim xmlDoc As New MSXML2.DOMDocument30
Dim currNode As IXMLDOMNode
Dim nodelist As IXMLDOMNodeList

xmlDoc.async = False
xmlDoc.Load "C:\XMLMigration\gm.xml"

  Set currNode = xmlDoc.documentElement

  Set nodelist = currNode.childNodes

  For Each currNode In nodelist
    MsgBox "Node name: " & currNode.nodeName
  Next



Mon, 06 Feb 2012 06:56:27 GMT  
 msxml DOM: reading each element in an XML
Hi David,

Each node can have ChildNodes so to iterate the tree properly you need to
use a recursive function, eg:

Sub RecurseChildren(ByVal nd as IXMLDOMNode)
   Dim child as IXMLDOMNode
   For each child in nd.childNodes
      Debug.Print "Node name: " & currNode.nodeName
         Dim grandKids As IXMLDOMNodeList
        grandKids = child.childNodes
        If Not grandKids Is Nothing Then
            Dim grandChild as IXMLDOMNode
           For each grandChild in grandKids
               RecurseChildren grandChild
           Next
        End If
    Next

End Sub

Or something like that.  (note I haven't tested the above, it was written
into live  mail not the VB IDE)


Quote:
> I am trying to read an XML (no other related files *.xsd...) with
> MSXML DOM but I am trying to figure out how to navigate the file so
> that I can access each element.

> I am trying to read each element sequentially from top to bottom and
> if there are elements with children <validation> I would want to read
> those in as well.  When the children are complete it would go back to
> the next sibling <site> after <validation>.  For some reason last week
> I am almost sure I was doing it with the code listed but today when I
> try it the <validation> sub-elements get skipped.

> In code below I cut and pasted the code to show the essential
> information.  I knew it was working because in my 'For Each' loop I
> was reading in each element and setting the .text propery to a value
> based on a simple text file that I would read 1 line of data which
> contains only one value and put that value into the XML with the
> currNode.Text = strValue.  Each tag from top to bottom was being
> written to.

> thanks for any help. David

> XML:
> <template>
> <validation>
> <request>T09</request>
> <per_number>2009</per_number>
> </validation>
> <site>-</site>
> <end_date>-</end_date>
> </template>

> vb6 code

> Dim xmlDoc As New MSXML2.DOMDocument30
> Dim currNode As IXMLDOMNode
> Dim nodelist As IXMLDOMNodeList

> xmlDoc.async = False
> xmlDoc.Load "C:\XMLMigration\gm.xml"

>  Set currNode = xmlDoc.documentElement

>  Set nodelist = currNode.childNodes

>  For Each currNode In nodelist
>    MsgBox "Node name: " & currNode.nodeName
>  Next



Mon, 06 Feb 2012 13:17:29 GMT  
 msxml DOM: reading each element in an XML
Bill,

Thanks for the info.

I am going to do it that way.  I have only been working with these XML
files for a very short time.  I had found similar code at Microsoft's
MSDN website (see link below).  But when I put some breakpoints on
some of the lines I was somewhat confused of what it was reading.
Partly because I did not know what a 'node' consisted of and even now
I am still learning.  The node can be of different types (see link
below) so you may have to account for that as well.  In my code I have
some nodes that have CDATA so the original MS code did not show me
those values so I had to change the IF statement: 'If xNode.nodeType =
NODE_TEXT Then' so I had to add NODE_CDATA_SECTION to that IF
statement.

Like I said before I thought my old working code went thru the whole
XML file from top to bottom and it all worked so that somewhat delayed
me getting a solution but most of the code I found showed me that I
was going to have to navigate thru parents/children.

Anyway thanks for reinforcing the thought that recursion is what I was
going to have to use.

sample code with recursion
http://msdn.microsoft.com/en-us/library/aa468547.aspx
click on link 'How Do I Traverse a Document' or google search 'If
xNode.nodeType = NODE_TEXT Then' in case that page goes away. I think
I saw other websites duplicate this code.

nodetype
http://msdn.microsoft.com/en-us/library/ms753745%28VS.85%29.aspx



Mon, 06 Feb 2012 21:36:57 GMT  
 msxml DOM: reading each element in an XML
If you are going to reply on XML in a significant way, you should consider
using XPath statements to read and write XML

http://www.w3schools.com/xpath/xpath_syntax.asp

--
2025
If you do not believe in time travel,
your beliefs are about to be tempered.

http://www.facebook.com/group.php?gid=43606237254

| Bill,
|
| Thanks for the info.
|
| I am going to do it that way.  I have only been working with these XML
| files for a very short time.  I had found similar code at Microsoft's
| MSDN website (see link below).  But when I put some breakpoints on
| some of the lines I was somewhat confused of what it was reading.
| Partly because I did not know what a 'node' consisted of and even now
| I am still learning.  The node can be of different types (see link
| below) so you may have to account for that as well.  In my code I have
| some nodes that have CDATA so the original MS code did not show me
| those values so I had to change the IF statement: 'If xNode.nodeType =
| NODE_TEXT Then' so I had to add NODE_CDATA_SECTION to that IF
| statement.
|
| Like I said before I thought my old working code went thru the whole
| XML file from top to bottom and it all worked so that somewhat delayed
| me getting a solution but most of the code I found showed me that I
| was going to have to navigate thru parents/children.
|
| Anyway thanks for reinforcing the thought that recursion is what I was
| going to have to use.
|
| sample code with recursion
| http://msdn.microsoft.com/en-us/library/aa468547.aspx
| click on link 'How Do I Traverse a Document' or google search 'If
| xNode.nodeType = NODE_TEXT Then' in case that page goes away. I think
| I saw other websites duplicate this code.
|
| nodetype
| http://msdn.microsoft.com/en-us/library/ms753745%28VS.85%29.aspx



Tue, 07 Feb 2012 00:17:16 GMT  
 msxml DOM: reading each element in an XML
One last thing I wanted to mention is about whitespace affecting my
TEXT nodes.

I used the following code to go thru all my Elements and edit the Text/
CDATA nodes.  However, I was having a problem if I had
'preservewhitespace=true' and the original XML had LF after each
line.  BTW, the file is provided to me by an outside company so it is
not up to me to change it.  If I had the 'preserve... property set to
false the first TEXT node it would find would be the one corresponding
to <number1> which is what I want, however if set to TRUE the first
TEXT node would be <validation> and if I checked the contents of the
TEXT node it would have a linefeed (chr(10) in it.  So what I had to
do is to add a condition like 'If INSTR(node.nodevalue,chr(10)) then
ignore...'.

I have very little XML experience so I don't know if this is the right
approach with DOM but it works and figuring it out that the
preservewhitespace property was the difference put me at ease that I
knew why it was happening.

David

sample XML
    <validation>
        <number1>abcd</number2>
        <number2>1234</number2>
    </validation>

Code I found on the MSDN website:

Public Sub DisplayNode(ByRef Nodes As MSXML2.IXMLDOMNodeList, ByVal
Indent As Integer)

    Dim xNode As MSXML2.IXMLDOMNode
    Indent = Indent + 2

    For Each xNode In Nodes
        If xNode.nodeType = NODE_TEXT Or xNode.nodeType =
NODE_CDATA_SECTION Then
          Debug.Print Space$(Indent) & xNode.parentNode.nodeName & _
                ":" & xNode.nodeValue
        End If

        If xNode.hasChildNodes Then
            DisplayNode xNode.childNodes, Indent
        End If
    Next xNode

End Sub



Thu, 09 Feb 2012 23:50:15 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Read XML using VB6 DOM 4.0 with Namespaces - See Sample XML

2. Read XML elements into Combo Box

3. Adding prolog information using MSXML 4.0 (April release) for a DOM document

4. Read XML elements into a Combo Box

5. DOM issue with MSXML

6. Read XML elements into Combo Box

7. msxml DOM: no linefeed after creating a new node

8. XML - using the MSXML parser in Access 97

9. difference between system.xml to msxml

10. MSXML.DLL load a xml string

11. XML and VB6 - using MSXML class to extract attribute data

12. XML in VB without MSXML

 

 
Powered by phpBB® Forum Software