我正在向第三方API发送XML请求,并且收到响应,我需要解析该响应以获取错误消息或获取成功和订单ID。
这是我从错误消息中得到的回复。
<!--?xml version="1.0" encoding="UTF-8"?-->
<soap:envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:body>
<createbackgroundcheckresponse xmlns="https://www.website.com/sif/ws/hrxml/2.0">
<applicationacknowledgement>
<payloadresponsesummary>
<payloaddisposition>
<entitydisposition>
<entityinstancexpath>/Order/SearchPackage[1]/ReferenceId[1]/IdValue[1] </entityinstancexpath>
<entityexception>
<exception>
<exceptionidentifier>02x0033</exceptionidentifier>
<exceptionseverity>Fatal</exceptionseverity>
<exceptionmessage>An order is already being processed with the same user</exceptionmessage>
</exception>
</entityexception>
</entitydisposition>
</payloaddisposition>
</payloadresponsesummary></applicationacknowledgement>
</createbackgroundcheckresponse>
</soap:body>
</soap:envelope>
我能够获得响应并将其写入浏览器,但无法解析它。这是我发送邮件时所拥有的。
Set xmlHttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlHttp.Open "POST", xUrl, False 'False = Synckronous Process
xmlHttp.SetRequestHeader "Content-type", contentType
xmlHttp.SetRequestHeader "Content-Length", Len(xmlBody)
xmlHttp.SetOption 2, 13056 'Ignore SSL errors
xmlHttp.Send xmlBody
'Get the response back
sRet = xmlHttp.ResponseText
If xmlHttp.ReadyState = 4 And xmlHttp.Status = 200 Then
HttpPOST = sRet
Else
HttpPOST = "[Http-Failure] " & vbNewLine & sRet
End If
一旦我取回数据,这里就是我所拥有的。
Set xmlDoc = CreateObject("Msxml2.DOMDocument.3.0")
xmlDoc.Async = False
xmlDoc.ResolveExternals = False
xmlDoc.SetProperty "ServerHTTPRequest", True
xmlDoc.ValidateOnParse = True
xmlDoc.PreserveWhiteSpace = True
If xmlDoc.LoadXML(sRet) Then
'Dim xmlNodeList
Set xmlNodeList = xmlDoc.GetElementsByTagName("exceptionmessage")
If Not (xmlNodeList Is Nothing) Then
response.Write "exceptionmessage is Not Nothing<P>"
If xmlNodeList.Length > 0 Then
response.Write "xmlNodeList length > 0<P>"
Response.Write "exceptionmessage Value is : " & xmlNodeList(0).Text & vbNewLine & "<BR>"
Else
Response.Write "exceptionmessage length is 0<P>"
End If
Else
response.Write "exceptionmessage is Nothing<P>"
End If
Set nodeXML = Nothing
End If
当我运行时,它将超越xmlDoc.LoadXML()
。然后它将超过If Not (xmlNodeList is Nothing) Then
的第一张支票。但是那边的长度是0,所以无法通过该部分。
答案 0 :(得分:0)
这里发生了几件事。
1)通过查看LoadXML的返回代码来检查XML解析器中的错误处理,然后检查如果返回码为false,则检查parseError对象以找出问题所在。
2)SOAP XML使用名称空间(不必要的IMHO)来查询名称空间,我使用XPath(SelectionLanguage = XPath),然后将SelectionNameSpaces设置为URL。您的XML使用“默认”名称空间,但是要查询它,您需要为其命名,我选择了“ ns”,但可以是任何东西。最后,使用selectNodes或selectSingleNode查询和检查结果。
Dim xml As Object
Set xml = CreateObject("MSXML2.DOMDocument")
Dim bReturn As Boolean
bReturn = xml.Load("c:\\temp\testsoap.xml")
If (bReturn = False) Then
MsgBox ("Error loading XML Response: " + CStr(xml.parseError.errorCode) + " " + xml.parseError.reason)
End If
Call xml.setProperty("SelectionLanguage", "XPath")
Call xml.setProperty("SelectionNamespaces", "xmlns:ns='https://www.website.com/sif/ws/hrxml/2.0'")
Dim ndException As Object
Set ndException = xml.selectSingleNode("//ns:exception")
If (ndException Is Nothing) Then
' it worked
Else
MsgBox ("Exception found in XML: " + ndException.Text)
End If