Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
718 views
in Technique[技术] by (71.8m points)

reading xml file with vbscript

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...