2012-01-18 14 views
2

MSXML2とXPathでVBAを解析するのにVBAを使用していますが、XPathクエリはさまざまなツールで動作しますが、selectNodesメソッドでは何も選択しません。私が今までに見た多くの質問から集めたものから、名前空間を指定する代わりにこの 'local-name'構文を使用できますが、何も選択されていません。私は間違って何をしていますか? (既にコメントで提案されているように、load代わりのloadXMLとし、必要に応じasync設定)私は仕事べきではないと私が持っているとき、私は、問題を再現できない理由コードが表示されないMSXML2でXPathが失敗する

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文書マークアップを持つ文字列を要求します。ファイル名をお持ちの場合は、( '.async = False'設定の後に)' load'メソッドを使用してください。 –

+0

sFilename =テキストファイルとしての石鹸データ。 '.pareseError.ErrorCode'と' .parseError.reason'はそれぞれ '-1072896682'と'ドキュメントのトップレベルで無効です 'を返しました。代わりに '.load 'メソッドを使うとエラーは出ませんが、まだノードは見つかりません。 – Todd

答えて

1

<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 

としてtest2012011901.xmlを提出後、両方のXPath式は2を見つけます出力が示すようの要素:だから、限り、私はあなたの問題を解決すべき問題が解決しない場合、あなたは手の込んだする必要がありますコメントに私の提案を伝えることができるよう

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.xml'の代わりに 'item.nodevalue'を見ようとしていたので、昨日気付かなかっただけです。明確化のためにありがとう。 – Todd

関連する問題