【发布时间】:2018-10-13 19:34:03
【问题描述】:
我正在向第三方 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,所以它不能通过那部分。
【问题讨论】:
-
VBScript 的 XML 解析器在遇到错误时会静默失败。检查
xmlDoc.ParseError的值。如果结果不为 0,请检查xmlDoc.ParseError.Reason的值。 -
VBScript 没有“XML 解析器”。您似乎在谈论 MSXML v. 3 DOM,它是 Windows 的一部分,而不是 VBScript,它不是解析器,而是 DOM。在 VBScript 中处理 SOAP 的首选方法是使用已弃用的 SOAP Toolkit 3.0,它在比通用 XML 处理更高的级别上运行。您还可以考虑使用 3rd 方 PocketSOAP 库。
标签: xml vbscript xml-parsing asp-classic msxml