如何从 WinHttp.WinHttpRequest 对象解析 xml?
我有一个独立的 VBScript,它连接到服务器并获取 WinHttpRequest 对象中的响应文本(作为 XML)。现在,我的问题是如何解析其中的 XML 内容。当我发布请求(strPostData)时,我需要解析响应 XML。我下面使用的不起作用,因为我无法在控制台上打印输出。我可以输出 ResponseText。但我无法解析它。
Set objWinHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
objWinHttp.Send(strPostData)
objWinHttp.WaitForResponse()
If objWinHttp.Status = "200" Then
GetDataFromURL = objWinHttp.ResponseText
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.loadXML(GetDataFromURL)
Set ops = xmlDoc.getElementsByTagName("Response\Status").item(0).text
WScript.Echo "Output is: " & ops
WScript.Echo "Message: " & GetDataFromURL
Msgbox GeteDataFromURL
WScript.Quit(0)
这是要解析的 XML:
<RCTRequest>
<Response>
<Name>aaa</Name>
<Status>44</Status>
</Response>
</RCTRequest>
I have a standalone VBScript which connects to server and gets the response text(as XML) in a WinHttpRequest object. Now, my question is how do I parse the XML content in it. When I post a request(strPostData) I need to parse the response XML. What I am using below is not working as I'm unable to print output on the console. I'm able to output the ResponseText though. But I'm unable to parse it.
Set objWinHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
objWinHttp.Send(strPostData)
objWinHttp.WaitForResponse()
If objWinHttp.Status = "200" Then
GetDataFromURL = objWinHttp.ResponseText
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.loadXML(GetDataFromURL)
Set ops = xmlDoc.getElementsByTagName("Response\Status").item(0).text
WScript.Echo "Output is: " & ops
WScript.Echo "Message: " & GetDataFromURL
Msgbox GeteDataFromURL
WScript.Quit(0)
Here is the XML to be parsed:
<RCTRequest>
<Response>
<Name>aaa</Name>
<Status>44</Status>
</Response>
</RCTRequest>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 XPath
You can use XPath
我想您会收到“运行时错误:需要对象”错误。这是由
Just remove
set
从该行开头引起的。I suppose you get "runtime error: Object required" error. It is caused by the line
Just remove
set
from the beginning of that line.使用 XMLDOM 您走在正确的道路上。查看我的文章在 WSH 中读取 XML 文件< /a> 有关如何从 XML 输入解析特定数据的示例。
You're on the right track using XMLDOM. Check out my article Reading XML Files in WSH for examples on how to parse specific data from an XML input.