【问题标题】:Parse XML error in VBScript在 VBScript 中解析 XML 错误
【发布时间】:2012-10-05 15:06:43
【问题描述】:

我有这个简单的VBScript,它发送一个HTTP POST 请求并读取返回的HTML 响应。

Function httpPOST(url, body, username, password )  
  Set Http = CreateObject("Msxml2.ServerXMLHTTP")   
  Http.Open "POST", url, False, username, password  
  Http.setRequestHeader _  
              "Content-Type", _  
              "application/x-www-form-urlencoded"  
  Http.send body 
  pagestatus = Http.status
  if pagestatus<> "200" then
    httpPOST="Error:"& pagestatus
  else
    'httpPOST = Http.ResponseBody
    'httpPOST = Http.responseText
    Set objXMLDoc = CreateObject("MSXML.DOMDocument")
    objXMLDoc.async = False
    objXMLDoc.validateOnParse = False
    objXMLDoc.load(Http.ResponseBody)
    Set objNode = objXMLDoc.selectSingleNode("/html/body/center/img")
    httpPost = objNode.getAttribute("alt") 
  end if
End Function

HTML 响应格式如下:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>---</title>
    </head>
    <body>
        <center>
            <img alt="You are now connected" src="pages/GEN/connected_gen.png">
        </center>
    </body>
</html>

这个脚本的问题是它总是返回Error: Object required: 'objNode'

我尝试了很多 XML 解析器的变体,最后每次遇到与 XML 对象相关的相同错误时都放弃了。

【问题讨论】:

  • 当您尝试从文件加载而不是从网络加载时,它是否有效?你确定 html 没有定义任何 xmlns 吗?
  • 是的,如果我尝试在本地或从 Web 读取 xml,我会遇到同样的错误。此 html 是从浏览器的视图源中复制的。

标签: xml parsing vbscript xml-parsing


【解决方案1】:

您的第一个问题已解决here.load 需要“包含指定 XML 文件位置的 URL 的字符串”;所以使用 .loadXml 来检查是否Http.ResponseBody 包含MSXML?.DOMDocument 可以解析的数据(您的第二个问题)。

更新:

“有效”的东西(以及为什么):

  Dim sHTML : sHTML = readAllFromFile("..\data\02.html")
  WScript.Echo sHTML
  Dim oXDoc : Set oXDoc = CreateObject("MSXML2.DOMDocument")
  oXDoc.async = False
  oXDoc.validateOnParse = False
  oXDoc.setProperty "SelectionLanguage", "XPath"
  If oXDoc.loadXML(sHTML) Then
     Dim ndImg : Set ndImg = oXDoc.selectSingleNode("/html/body/center/img")
     Dim httpPost : httpPost = ndImg.getAttribute("alt")
     WScript.Echo "ok", httpPost
  Else
     WScript.Echo "Error: " & trimWS(oXDoc.parseError.reason)
  End If

输出:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>---</title>
    </head>
    <body>
        <center>
            <img alt="You are now connected" src="pages/GEN/connected_gen.png"/>
        </center>
    </body>
</html>

ok You are now connected

MSXML2.DOMDocument.loadXML(并解析)HTML 代码,前提是它是“XML 有效的”。您的 HTML 不是,因为 img 标记未关闭 - 我为您的原始代码收到的错误消息:

Error: End tag 'center' does not match the start tag 'img'.

如何进行进一步取决于您是否能够/愿意更改 HTML。

更新二:

虽然您可以在将 .ResponseBody 之前 提供给 .loadXML 之前对它做一些讨厌的事情 - 为什么不使用 HTML 工具来解析 HTML:

  Dim sHTML : sHTML = readAllFromFile("..\data\01.html")
  WScript.Echo sHTML
  Dim oHF : Set oHF = CreateObject("HTMLFILE")
  oHF.write sHTML
  Dim httpPost : httpPost = oHF.documentElement.childNodes(1).childNodes(0).childNodes(0).alt
  WScript.Echo "ok", httpPost

输出:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>---</title>
    </head>
    <body>
        <center>
            <img alt="You are now connected" src="pages/GEN/connected_gen.png">
        </center>
    </body>
</html>

ok You are now connected

如输出所示,HTMLFILE 接受您的 'not-xml-closed' img;当然,获得你真正想要的东西的方法应该经过消毒。

【讨论】:

  • 感谢您的回复。正如我在问题中所说,我尝试了很多变体,而您发布的链接就是其中之一。我认为您指的是Msxml2.DOMDocument 而不是Msxml2.ServerXMLHTTP。它可能最终会解决您指出的第二个问题。
  • @update:从技术上讲,html 是有效的,img 标签是关闭的。可能是解析器问题。你知道我可以使用的其他任何一个吗,因为我无法控制 html 响应。
  • @update ii:工作得很好。这就是我最终要做的,感谢您的帮助。
猜你喜欢
  • 2012-11-18
  • 1970-01-01
  • 2011-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多