2012-01-18 93 views
2

我使用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> 
+0

'loadXML'調用後檢查'.parseError.errorCode'和'.parseError.reason'。 'loadXML sFileName'看起來不正確,'loadXML'方法需要一個帶有XML文檔標記的字符串,而不是文件名。如果你有一個文件名,那麼使用'load'方法(在設置'.async = False'後)。 – 2012-01-18 16:13:40

+0

sFilename =肥皂數據作爲文本文件。 '.pareseError.ErrorCode'和'.parseError.reason'分別返回「-1072896682」和「文檔頂層無效」。如果我使用'.load'方法,而不是錯誤,但仍然沒有找到節點。 – Todd 2012-01-18 20:24:09

回答

1

我不明白爲什麼代碼(與load而不是loadXML和必要async設置,因爲已經在評論建議)不應該工作,我不能重現該問題,當我有文件test2012011901.xml

<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> 

和VBScript代碼

Dim doc, items 
Set doc = CreateObject("Msxml2.DOMDocument.6.0") 
doc.async = False 
If doc.load("test2012011901.xml") Then 
    Set items = doc.selectNodes("//item") 
    WScript.Echo "Found " & items.length & " element(s)." 
    For Each item In items 
    WScript.Echo item.xml 
    Next 
    Set items = doc.selectNodes("//*[local-name() = 'item']") 
    WScript.Echo "Found " & items.length & " element(s)." 
    For Each item In items 
    WScript.Echo item.xml 
    Next 
Else 
    WScript.Echo "Parse error " & doc.parseError.reason 
End If 

那麼這兩個XPath表達式找到兩個item種元素,作爲輸出顯示:

Found 2 element(s). 
<item> 
     // some elements 
    </item> 
<item> 
    // some elements 
    </item> 
Found 2 element(s). 
<item> 
     // some elements 
    </item> 
<item> 
    // some elements 
    </item> 

所以,據我可以告訴我的建議,評論應該解決您的問題,您需要詳細說明,如果你仍然有問題。

+0

更改爲'load'方法的確的工作,我昨天沒有注意到它,因爲我也想看看'item.nodevalue'而不是'item.xml'。感謝澄清。 – Todd 2012-01-19 13:17:06