使用VBA解析XML SOAP文档

时间:2019-03-07 10:16:11

标签: xml vba soap web-scraping

我将不胜感激。我需要解析XML SOAP文档,但总是得到相同的结果->没有标签的纯文本。如何从XML获取特定/单个标记(例如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>

1 个答案:

答案 0 :(得分: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