如何从 WinHttp.WinHttpRequest 对象解析 xml?

发布于 2024-12-26 06:16:39 字数 939 浏览 2 评论 0原文

我有一个独立的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半﹌身腐败 2025-01-02 06:16:39

您可以使用 XPath

  Set xmlDoc = CreateObject("Microsoft.XMLDOM")
  xmlDoc.async = false
  xmlDoc.SetProperty "SelectionLanguage", "XPath"      
  xmlDoc.loadXML(GetDataFromURL)
  Set ops =xmlDoc.SelectSingleNode("/RCTRequest/Response/Status")
  WScript.Echo "Output is: " &  (ops.text)
  WScript.Echo "Message: " & GetDataFromURL
  Msgbox GeteDataFromURL
  WScript.Quit(0)   

You can use XPath

  Set xmlDoc = CreateObject("Microsoft.XMLDOM")
  xmlDoc.async = false
  xmlDoc.SetProperty "SelectionLanguage", "XPath"      
  xmlDoc.loadXML(GetDataFromURL)
  Set ops =xmlDoc.SelectSingleNode("/RCTRequest/Response/Status")
  WScript.Echo "Output is: " &  (ops.text)
  WScript.Echo "Message: " & GetDataFromURL
  Msgbox GeteDataFromURL
  WScript.Quit(0)   
随波逐流 2025-01-02 06:16:39

我想您会收到“运行时错误:需要对象”错误。这是由

Set ops = xmlDoc.getElementsByTagName("Response\Status").item(0).text

Just remove set 从该行开头引起的。

I suppose you get "runtime error: Object required" error. It is caused by the line

Set ops = xmlDoc.getElementsByTagName("Response\Status").item(0).text

Just remove set from the beginning of that line.

久而酒知 2025-01-02 06:16:39

使用 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.

心在旅行 2025-01-02 06:16:39
strFile = "inp.xml"
Set objFS = CreateObject( "Scripting.FileSystemObject" )
set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load(strFile)
For each x in xmlDoc.documentElement.attributes
  WScript.Echo x.nodeName, x.text
Next

set xmlCol = xmlDoc.documentElement.childNodes
For Each Elem In xmlCol 
 If StrComp(Elem.nodeName, "p") = 0 Then
  set nestedChild = Elem.childNodes

  For Each node In nestedChild 
    If StrComp(node.nodeName, "XYZ") = 0 Then
      WScript.Echo Elem.xml
      set a = objFS.CreateTextFile("testfile.txt", true)
      a.WriteLine(Elem.xml)
      a.Close()
    End If
  Next

 End If
Next

strFile = "inp.xml"
Set objFS = CreateObject( "Scripting.FileSystemObject" )
set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load(strFile)
For each x in xmlDoc.documentElement.attributes
  WScript.Echo x.nodeName, x.text
Next

set xmlCol = xmlDoc.documentElement.childNodes
For Each Elem In xmlCol 
 If StrComp(Elem.nodeName, "p") = 0 Then
  set nestedChild = Elem.childNodes

  For Each node In nestedChild 
    If StrComp(node.nodeName, "XYZ") = 0 Then
      WScript.Echo Elem.xml
      set a = objFS.CreateTextFile("testfile.txt", true)
      a.WriteLine(Elem.xml)
      a.Close()
    End If
  Next

 End If
Next

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文