Skip navigation
BigGunN
Currently Being Moderated

Loop through XML

Feb 1, 2012 4:36 PM

I am looking for an example of how to loop through an xml structure, preferrably in VBScript, but I will take a Javascript example and try to figure out how to adapt it.

 

Is a FOR EACH loop possible? In normal VBScript you can do something like this...

 

‘strQuery = “/ProfilesInfo/Profiles/ProfileInfo/Profile/Fields/Field [Name='ProfileUri' or Name= 'Age'] /(Name|Value)”

Set colNodes = xmlDoc.selectNodes( strQuery )

If colNodes.length>0 Then

For Each objNode in colNodes

Print objNode.nodeName & “: ” & objNode.text

Next

 

is it possible to do something like this with XML? Maybe something like

 

For each node in myDocument.XMLElements.Item(1)

     {do something}

Next

 

If there is a better way, and I am sure there is, I'll take an example of that instead. 

 

I am sure there is probably a way to do it with XPath, but I haven't been able to find any examples of using XPath with VBScript. 

 

If anyone has an example of how to use XPath with VBScript I would greatly appreciate it.

 

Thank you

 
Replies
  • John Hawkinson
    5,572 posts
    Jun 25, 2009
    Currently Being Moderated
    Feb 1, 2012 10:51 PM   in reply to BigGunN
     
    |
    Mark as:
  • Currently Being Moderated
    Feb 1, 2012 11:01 PM   in reply to BigGunN

    You can do this using XML rules. Check out a chapter of the same name in the InDesign CS5 Scripting Guide (VB version). There you will find many examples.

    You can indicate which nodes you want to process with XPath. For example, this will match on every XML element in the XML structure:

    Public Property Get xpath
         xpath = "//*"
    End Property
    

     

    And this matches all following sibling XML elements of the first "item" XML element:

    Public Property Get xpath
         xpath = "/xmlElement/item[1]/following-sibling::*"
    End Property
    
     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points