7

我正在尝试编写一个 vbscript 来自动配置存储阵列。我在弄清楚如何最好地导航 XML 时遇到了一些困难。

我的 XML 的示例部分:

<SERVER>
<INTERFACE>
<PORT>0</PORT>
<IPADDRESS>192.168.1.1</IPADDRESS>
<NETMASK>255.255.255.0</NETMASK>
</INTERFACE>
<INTERFACE>
<PORT>1</PORT>
<IPADDRESS>192.168.1.2</IPADDRESS>
<NETMASK>255.255.255.0</NETMASK>
</INTERFACE>
</SERVER>

所以我想遍历每个接口(实际上有 5 个)并在正确的接口上设置适当的 IP 和网络掩码。

我目前正在这样做:

Set objXMLDoc = CreateObject("Microsoft.XMLDOM") 
objXMLDoc.async = False 
objXMLDoc.load("example.xml")

Set Root = objXMLDoc.documentElement 
Set NodeList = Root.getElementsByTagName("interface") 
port = 0
For Each Elem In NodeList 
WScript.Echo "Port " & port & " has IP address of " & Elem.text
port = port + 1
Next

但是必须有一种更简洁的方法来执行此操作,我可以选择接口部分并读取端口、IP 地址和网络掩码,发出命令然后进入下一个接口?

谢谢。

4

2 回答 2

6

第一种方法:

For Each Elem In NodeList 
   SET port = Elem.getElementsByTagName("Port")(0)
   SET ip = Elem.getElementsByTagName("IPADDRESS")(0)
   WScript.Echo "Port " & port.nodeValue & " has IP address is " & ip.nodeValue
Next
于 2011-11-24T09:32:44.687 回答
2

这对我有用:

sub main
    Set nodeList = xmlDoc.documentElement.selectNodes("//interface")

    For Each node in nodeList
        handleNode(node)
    Next
end sub

sub handleNode(node)
    Dim port, ipaddress, netmask, attribute

    For each elem in node.childNodes
        Select Case node.tagName
            Case "port"
                port = elem.text
            Case "ipaddress"
                ipaddress = elem.text
            Case "netmask"
                netmask = elem.text
            Case "tag with attributes"
                attribute = elem.getAttribute("attributeName")
        End Select
    Next

    WScript.Echo "Port " & port & " has IP address of " & ipaddress & " and useful attribute " & attribute

end sub
于 2016-03-04T12:40:39.090 回答