我使用VBA解析MSXML2和XPath的soap xml,但XPath查詢在各種工具中工作時不會在selectNodes方法中選擇任何內容。從我迄今爲止所看到的衆多問題中所收集的內容中,我可以使用這種「本地名稱」語法,而不是指定命名空間,但沒有任何選擇。我究竟做錯了什麼?XPath失敗,MSXML2
private const DQ = """"
Public Sub parseXML(sFileName As String)
Dim xmldoc As New MSXML2.DOMDocument60, I As IXMLDOMNodeList, x As IXMLDOMNode
With xmldoc
.loadXML sFileName
.SetProperty "SelectionLanguage", "XPath"
Set I = .selectNodes("//*[local-name()=" & DQ & "item" & DQ & "]")
If I.length > 0 Then
' do something useful
end if
End With
End Sub
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Header></soap-env:Header>
<soap-env:Body>
<n0:ZBexQaasResponse xmlns:n0="urn:sap-com:document:sap:soap:functions:mc-style">
<Messages>
<item>
// some elements
</item>
</Messages>
<OutputTable>
<item>
// some elements
</item>
</OutputTable>
<TextElements></TextElements>
<XmlOutput></XmlOutput>
<XmlTxtelem></XmlTxtelem>
</n0:ZBexQaasResponse>
</soap-env:Body>
</soap-env:Envelope>
'loadXML'調用後檢查'.parseError.errorCode'和'.parseError.reason'。 'loadXML sFileName'看起來不正確,'loadXML'方法需要一個帶有XML文檔標記的字符串,而不是文件名。如果你有一個文件名,那麼使用'load'方法(在設置'.async = False'後)。 – 2012-01-18 16:13:40
sFilename =肥皂數據作爲文本文件。 '.pareseError.ErrorCode'和'.parseError.reason'分別返回「-1072896682」和「文檔頂層無效」。如果我使用'.load'方法,而不是錯誤,但仍然沒有找到節點。 – Todd 2012-01-18 20:24:09