Loop through a childNodes children

Posted on 2004-04-20
Last Modified: 2010-08-05
            <pic filename="1.jpg"/>
            <pic filename="2.jpg"/>
            <pic filename="3.jpg"/>

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

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


Question by:speder
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3

Expert Comment

ID: 10872678
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

Expert Comment

ID: 10872695
Should have added, assuming oDOM has been created as MSXML2.DOMDocument and your XML has been loaded into the oDOM object.

Author Comment

ID: 10872717
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.

Accepted Solution

sbennetts earned 80 total points
ID: 10872784
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
     end if

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Microsoft Access combo box help 2 57
Child Form in front 4 66
VB6 - Scroll Mouse wheel on Picturebox 13 121
VBA: Insert New column with specific format type 12 56
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

751 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