xml 用vbscript读取xml文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8254600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
reading xml file with vbscript
提问by knoxvillain
I am trying to write a vbscript to automate the configuration of a storage array. I'm having some difficulty figuring out how best to navigate the XML.
我正在尝试编写一个 vbscript 来自动配置存储阵列。我在弄清楚如何最好地导航 XML 时遇到了一些困难。
An example section of my 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>
So I want to iterate through each interface (there is 5 in reality) and set the appropriate IP and netmask on the correct interface.
所以我想遍历每个接口(实际上有 5 个)并在正确的接口上设置适当的 IP 和网络掩码。
I'm currently doing this:
我目前正在这样做:
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
but there must be a cleaner way do doing this where I can select the interface section and read in the port, ipaddress & netmask, issue the command and then move into the next interface?
但必须有一种更简洁的方法来做到这一点,我可以选择接口部分并读取端口、IP 地址和网络掩码,发出命令,然后进入下一个接口?
Thanks.
谢谢。
回答by dani herrera
First approach:
第一种方法:
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
回答by WorkingMatt
This works for me:
这对我有用:
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

