Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Loop through a childNodes children

Posted on 2004-04-20
4
Medium Priority
?
651 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 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…
Suggested Courses

782 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