Solved

Loop through a childNodes children

Posted on 2004-04-20
4
627 Views
Last Modified: 2010-08-05
<articles>
      <data/>
      <headline/>
      <media>
            <pic filename="1.jpg"/>
            <pic filename="2.jpg"/>
            <pic filename="3.jpg"/>
      </media>
</article>

If I can loop through the article childNodes with this:

   Set root = xmlDoc.documentElement
   Set oNodeList = root.childNodes
   For Each Item In oNodeList
      MsgBox Item.xml
   Next

Then how can I loop through the media childNodes and access the filename attribute. Please put the code inside the other loop.

Thanks,

Soren
0
Comment
Question by:speder
  • 3
4 Comments
 
LVL 2

Expert Comment

by:sbennetts
Comment Utility
I would use an XPath query to select the media element and then loop through those as follows:

    Dim oEle           As MSXML2.IXMLDOMElement
    Dim MyNodeList     As MSXML2.IXMLDOMNodeList
   
    Set MyNodeList = oDOM.selectNodes("articles/media/pic")
    For Each oEle In MyNodeList
        sFileName = oEle.Attributes.getNamedItem("filename").Text
    Next
0
 
LVL 2

Expert Comment

by:sbennetts
Comment Utility
Should have added, assuming oDOM has been created as MSXML2.DOMDocument and your XML has been loaded into the oDOM object.
Simon.
0
 

Author Comment

by:speder
Comment Utility
Nah, I thought about that as well, but I'm already in the one loop and would like to integrate the other loop in the first one.
0
 
LVL 2

Accepted Solution

by:
sbennetts earned 80 total points
Comment Utility
Are you looking for a full XML parser or something specific for your XML file?

If it is specific to your XML, you could use something like...

    Dim oEle           As MSXML2.IXMLDOMElement
    Dim MyNodeList     As MSXML2.IXMLDOMNodeList
   
   For Each Item In oNodeList
     if Item.selectnodes("media/pic").length>0 then
        Set MyNodeList = Item.selectNodes("articles/media/pic")
        For Each oEle In MyNodeList
            sFileName = oEle.Attributes.getNamedItem("filename").Text
        Next
     end if
   Next


Also, your XML is not well formed, but I assume that is a typo in here...
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now