Forum Discussion

jvalero's avatar
jvalero
Visitor
13 years ago

Parsing Child Node Attributes in XML

I have a section in my XML that looks like the following:
<adhocparams>
<param name="appname" value="headlines2"/>
<param name="sourceSite1" value="somesite.com"/>
<param name="sourceSite2" value="somesite.com"/>
<param name="sourceSite3" value="somesite.com"/>
<param name="sourceSite4" value="somesite.com"/>
<param name="ip" value="192.123.123.123"/>
</adhocparams>

What is the best way to parse out the value from a child node, for this example let's say the first param node.

I've tried many combinations to try and parse this value. With no luck. I was going to try using the GetAttributes method, but the documentation isn't complete for it and on my first few attempts it does not appear to work like JavaScript's getAttributes() which was what I was expecting.

Appreciate the help.

2 Replies

  • This code will print the value of the "value" attribute of each "param" node:

    xmlString = "<adhocparams>....</adhocparams>"
    xml = CreateObject("roXmlElement")
    If xml.Parse(xmlString) Then
    For Each paramNode In xml.param
    print paramNode@value
    Next
    End If

    The GetAttributes() method returns an associative array whose keys are the names of the attributes, so the GetAttributes() for the paramNode in your example would contain two keys: "name" and "value"
    You can access attributes either using the "@" shortcut notation in the above code (if there are no special characters in the attribute name), or via the GetAttributes() associative array.
  • Okay I got your example working. Appreciate the help. 😄