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
Solved

Loop through a childNodes children

Posted on 2004-04-20
4
634 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
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
    Next
0
 
LVL 2

Expert Comment

by:sbennetts
ID: 10872695
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
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.
0
 
LVL 2

Accepted Solution

by:
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
        Next
     end if
   Next


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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

828 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