Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Loop through a childNodes children

Posted on 2004-04-20
4
Medium Priority
?
646 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
[X]
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
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 320 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

715 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