使用vbscript读取xml文件
我正在尝试编写一个 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 地址和端口号。 netmask,发出命令,然后进入下一个界面?
谢谢。
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.
An example section of my 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.
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?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一种方法:
First approach:
这对我有用:
This works for me: