2016-12-13 102 views
0

我想從僅讀取1個XML文件的VBScript獲取輸出。 此XML文件位於C:\腳本\ license.xml如何使用VBScript讀取XML文件中的子標籤?

<?xml version="1.0" encoding="utf-8"?> 
<License xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsi="http://www.w3.org/2001/XM LSchema-instance" xmlns="http://tempuri.org/License.xsd"> 
<Network> 
<NetworkName>TestName</NetworkName> 
<NetworkID>29</NetworkID> 
<Region>1</Region> 
<MaxDisplays>375</MaxDisplays> 
<Expiry>2017-05-25</Expiry> 
<Issued>2016-12-11</Issued> 
<Communicator>ZZ007007007</Communicator> 

我想使用VBScript進行的zabbix監控到期標記在XML文件中。

Dim licDate 
Set xmlDoc = CreateObject("Msxml2.DOMDocument") 
xmlDoc.load("C:\scripts\license.xml") 

licDate = xmlDoc.getElementsByTagName("Expiry").item(0).text 

itemInfo= licDate 
MsgBox itemInfo 

似乎無法得到此代碼的工作。 我得到的錯誤:所需的對象: 'xmlDoc.getElementsByTagName(...)項(...)。'

回答

0

由於mentioned in the remarks section for getElementsByTagName on MSDN

The getElementsByTagName method simulates the matching of the provided argument against the result of the tagName property of IXMLDOMElement . When executed, it does not recognize or support namespaces. Instead, you should use the selectNodes method, which is faster in some cases and can support more complex searches.

因此你有兩種選擇 - 做的文檔狀態,或手動循環遍歷每個標籤。

xmlDoc.setProperty "SelectionNamespaces", "xmlns:default='http://tempuri.org/License.xsd'" 
Set expiry = xmlDoc.selectSingleNode("//default:Expiry") 
Wscript.Echo expiry.text 

2017-05-25


Set nodeList = xmlDoc.getElementsByTagName("*") 
For index = 0 To nodeList.Length - 1 
    Set element = nodeList.item(index) 
    If element.tagName = "Expiry" Then 
     Wscript.Echo element.text 
    End If 
Next