在 VBA 中解析 SOAP XML 响应

Parse SOAP XML Response in VBA

我们正在尝试解析以下响应并使用 VBA 代码从“结果”标签中获取值“123456”,但我们没有得到任何东西:

收到回复:

------=_Part_119884_965967558.1620391101235
Content-Type: application/xop+xml;charset=utf-8;type="text/xml"
Content-Transfer-Encoding: 8bit
Content-ID: <e119e24d-e0b6-4ceb-a00a-dd094ef91ef7>

<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:wsa="http://www.w3.org/2005/08/addressing">
    <env:Header>
        <wsa:Action>http://xxxx</wsa:Action>
        <wsa:MessageID>urn:uuid:xxxx</wsa:MessageID>
    </env:Header>
    <env:Body>
        <ns0:uploadFileResponse
            xmlns:ns0="http://xxxx">
            <result
                xmlns="http://xxxx">123456
            </result>
        </ns0:uploadFileResponse>
    </env:Body>
</env:Envelope>

------=_Part_119884_965967558.1620391101235--

VBA代码:

    Dim xmldoc As Object
    Dim xmlnode As Object
    Set xmldoc = CreateObject("Microsoft.XMLDOM")
    xmldoc.SetProperty "SelectionLanguage", "XPath"
    xmldoc.async = False
    xmldoc.LoadXML .ResponseText
    For Each xmlnode In xmldoc.SelectNodes("//*[contains(name(),'result')]")
        Debug.Print "Document ID: "; xmlnode.text
    Next

请帮忙

您需要清理响应。

    Dim xmldoc As Object
    Dim xmlnode As Object
    Set xmldoc = CreateObject("Microsoft.XMLDOM")
    xmldoc.SetProperty "SelectionLanguage", "XPath"
    xmldoc.async = False
    
    Dim xml As String
    xml = .ResponseText
    xml = Mid(xml, InStr(1, xml, "<?xml "))
    xml = Left(xml, InStr(1, xml, "Envelope>") + Len("Envelope"))

    If xmldoc.LoadXML(xml) = True Then
        For Each xmlnode In xmldoc.SelectNodes("//*[contains(name(),'result')]")
            Debug.Print "Document ID: "; xmlnode.Text
        Next
    Else
        MsgBox "XML failed to load", vbCritical
    End If