使用vbscript分析XML Soap响应



我正在向第三方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,所以它不能通过那个部分。

这里发生了一些事情。

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:\temptestsoap.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

最新更新