我正在使用供應商提供的 API,該 API 在我呼叫它時回傳此 XML。
<RESPONSE>
<VALID>true</VALID>
<MESSAGE></MESSAGE>
<DATA>
<WORK>
<ROW type="object" class="empty">
<brief_desc>Check plate</brief_desc>
<compid>1354</compid>
<comp_desc>Spitfire #2</comp_desc>
<initials></initials>
<sch_date>2019-10-01</sch_date>
<wo>43351</wo>
<workstatus>O</workstatus>
<work_desc>Check plate for flatness</work_desc>
</ROW>
<ROW type="object" class="empty">
<brief_desc>Check wheel</brief_desc>
<compid>1354</compid>
<comp_desc>Spitfire #1</comp_desc>
<initials></initials>
<sch_date>2019-10-08</sch_date>
<wo>43685</wo>
<workstatus>O</workstatus>
<work_desc>Check wheel for roundness</work_desc>
</ROW>
</WORK>
</DATA>
</RESPONSE>
我從另一個供應商提供的 HMI 軟體中呼叫它,該軟體僅支持 VBscript。我可以使用 Microsoft.XMLDOM 物件獲取這樣的單個值:
xmlDoc.documentElement.selectSingleNode("//VALID").text
但是,當我嘗試獲取“WORK”節點的集合時,就像這樣......
objNodeList = xmlDoc.getElementsByTagName("DATA")
...我收到一條錯誤訊息,指出“引數數量錯誤或屬性分配無效”。“DATA”元素全部大寫,所以我想我的名字是正確的。我對此進行了相當多的研究,但似乎無法找到具有完全相同問題的人。我將不勝感激任何人可能提出的任何建議。謝謝!
根據回復此帖子的人的要求,編輯并添加完整代碼:
Function EmaintTest
Dim objRequest
Dim strUrl
Dim strResponse
Dim body
Dim strResponseHeaders
Dim allResponseHeader
Dim xmlDoc
Set objRequest = CreateObject("MSXML2.XMLHTTP")
strUrl = "https://somewebsite.com/wc.dll?x3~api~&q=GetAnyData"
'Set body value as required by API.
body = "{""table"":""WORK"",""columns"":""WO,COMPID,COMP_DESC,WORKSTATUS,BRIEF_DESC,SCH_DATE,WORK_DESC,INITIALS"",""pageNumber"":1,""pageSize"":1000,""filter"":{""logic"":""and"",""filters"":[{""field"":""wo_type"",""operator"":""eq"",""value"":""PM""},{""field"":""COMPID"",""operator"":""eq"",""value"":""1354""},{""field"":""WORKSTATUS"",""operator"":""eq"",""value"":""O""}]},""sortBy"":[{""field"":""SCH_DATE"",""dir"":""asc""}]}"
With objRequest
.Open "POST", strUrl, False
.SetRequestHeader "Cache-Control", "no-cache"
.SetRequestHeader "Content-Type", "text/plain"
.SetRequestHeader "Accept", "application/xml"
.SetRequestHeader "Accept-Encoding", "gzip, deflate, br"
.SetRequestHeader "Connection", "keep-alive"
.Send body
strResponseHeaders = .StatusText
strResponse = .ResponseText
allResponseHeader = .GetAllResponseHeaders
End With
'Load the XML
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.loadXML(strResponse)
'Assign variables using the values returned from the API
$Trace (xmlDoc.documentElement.selectSingleNode("//VALID").text ) 'This works, returns "True" value.
Dim xmlNodes
Dim xmlNode
Set xmlNodes = xmlDoc.documentElement.selectNodes("DATA/WORK")
$Trace ("Using selectNodes...")
For Each xmlNode in xmlNodes
$Trace (xmlNode.text)
Next
$Trace ("Using getElementsByTagName...")
Set xmlNodes = xmlDoc.getElementsByTagName("DATA/WORK")
For Each xmlNode In xmlNodes
$Trace (xmlNode.text)
Next
End Function
uj5u.com熱心網友回復:
問題似乎與您尚未共享的代碼有關。以下代碼適用于您的 XML 檔案。請注意,您可以選擇使用selectNodes或getElementsByTagName。
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Load ".\Test.xml"
WScript.echo "Using selectNodes..."
Set xmlNodes = xmlDoc.documentElement.selectNodes("DATA/WORK")
For Each xmlNode in xmlNodes
WScript.echo xmlNode.text
Next
WScript.echo "Using getElementsByTagName..."
Set xmlNodes = xmlDoc.getElementsByTagName("DATA/WORK")
For Each xmlNode in xmlNodes
WScript.echo xmlNode.text
Next
這是一種遍歷所有節點的方法:
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Load ".\Test.xml"
Set xmlNodes = xmlDoc.selectNodes("//*")
For i = 0 to xmlNodes.length - 1
WScript.Echo xmlNodes(i).nodeName & ": " & xmlNodes(i).text
Next
uj5u.com熱心網友回復:
我建議使用不同的方法,即selectNodes().
objNodeList = xmlDoc.selectNodes("/RESPONSE/DATA")
或者
objNodeList = xmlDoc.selectNodes("/RESPONSE/DATA/WORK/ROW")
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/397757.html
上一篇:Arraylist不添加
