Search code examples
xmlvbscriptscriptingxmldom

VBScript XMLDOM


I am trying to retrieve all values from the XML below in VBScript with XMLDOM. Unfortunately, the nodes do not have the same name tag and the amount of tags (c[n]) is variable. How can I read the values to a dictionary?

I can get the tags by using:

Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "false"
xmlDoc.Load(xmlhttp.responseXML)

Set values = xmlDoc.getElementsByTagName("header")

How do I iterate all child nodes of <header>?

<table>
    <header>
        <c0 type="string">name</c0>
        <c1 type="ip_address">last_ip_address</c1>
        <c2 type="string">group_name</c2>
        <c3 type="enum">device_type</c3>
        <c4 type="string">os_version_and_architecture</c4>
        <c5 type="string">device_manufacturer</c5>
        <c6 type="integer">number_of_cpus</c6>
        <c7 type="string">cpu_model</c7>
        <c8 type="integer">number_of_cores</c8>
        <c9 type="mhz">cpu_frequency</c9>
        <c10 type="byte">total_ram</c10>
        <c11 type="integer">number_of_graphical_cards</c11>
        <c12 type="byte">graphical_card_ram</c12>
        <c13 type="datetime">last_system_boot</c13>
        <c14 type="datetime">last_logon_time</c14>
        <c15 type="string">bios_serial_number</c15>
        <c16 type="string">device_product_version</c16>
    </header>
</table>

Solution

  • Use Msxml2.DOMDocument instead of the deprecated Microsoft.XMLDOM, and use SelectNodes() with an XPath expression instead of getElementsByTagName().

    Set d = CreateObject("Scripting.Dictionary")
    For Each n In xmlDoc.SelectNodes("//table/header/*")
      d.Add n.NodeName, n.Text
    Next