【问题标题】:Parse XML SOAP document using VBA使用 VBA 解析 XML SOAP 文档
【发布时间】:2019-07-29 04:33:59
【问题描述】:

如果有任何帮助,我将不胜感激。我需要解析 XML SOAP 文档,但总是得到相同的结果 -> 没有标签的纯文本。如何从 XML 获取特定/单个标签 (f.e. vatNumber) 值?谢谢。

这是我的 VBA 代码:

ObjHTTP.Open "Post", sURL, False
ObjHTTP.setRequestHeader "Content-Type", "text/xml"
ObjHTTP.send (sEnv)

'parse xml response - output
Dim responseDocument As MSXML2.DOMDocument60
Set responseDocument = New MSXML2.DOMDocument60

responseDocument.async = False
responseDocument.validateOnParse = False
responseDocument.SetProperty "SelectionNamespaces", " xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'"    
responseDocument.LoadXML (ObjHTTP.responseText)

If responseDocument.parseError.reason <> "" Then
      MsgBox m_objDOMPeople.parseError.reason
     Exit Sub
End If

MsgBox responseDocument.SelectNodes("/soap:Envelope/soap:Body")(0).Text
Set ObjHTTP = Nothing
Set xmldoc = Nothing

这是 XML SOAP 输入:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
       <soap:Body>
          <checkVatResponse xmlns="urn:ec.europa.eu:taxud:vies:services:checkVat:types">
             <countryCode>SK</countryCode>
             <vatNumber>204566287588</vatNumber>
             <requestDate>2015-04-07+01:00</requestDate>
             <valid>true</valid>
             <name>Company k. s.</name>
             <address>Some Address</address>
          </checkVatResponse>
       </soap:Body>
    </soap:Envelope>

【问题讨论】:

    标签: xml vba soap web-scraping


    【解决方案1】:

    从 xpath 搜索中删除有问题的命名空间

    Set node = xmlDoc.SelectSingleNode("//*[local-name()='vatNumber']")
    Debug.Print node.Text
    

    或者从响应中删除第二个 xmlns 然后解析(例如,您可以正则表达式替换)

    Dim node As IXMLDOMElement
    Set node = xmlDoc.SelectSingleNode("//vatNumber")
    Debug.Print node.Text
    

    【讨论】:

    • 好的,谢谢。第一个选项效果很好!我删除了命名空间和 xmlns(整行),它可以工作。
    猜你喜欢
    • 2021-05-23
    • 2018-03-12
    • 2012-07-15
    • 1970-01-01
    • 2015-12-10
    • 2012-11-02
    • 2012-01-08
    • 1970-01-01
    • 2011-03-12
    相关资源
    最近更新 更多