VBScript 和 loadXML : Invalid at the top level of the document. 如何解决?

标签 vbscript xmldom

这是我在 stackoverflow 上的要塞帖子。我在这个网站上搜索了许多类似的问答,但我的情况似乎有点不同。这是我的 vbscript 代码:

------------ 代码片段----------------

xmlurl = "songs.xml"

set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.loadXML(xmlurl)

if xmlDoc.parseError.errorcode<>0 then
  'error handling code
  msgbox("error! " & xmlDoc.parseError.reason)
end if

------------结束代码片段----------------

XML:

<?xml version="1.0" encoding="UTF-8"?>
<nowplaying-info-list>
  <nowplaying-info mountName="CKOIFMAAC" timestamp="1339771946" type="track">
    <property name="track_artist_name"><![CDATA[CKOI]]></property>
    <property name="cue_title"><![CDATA[HITMIX]]></property>
  </nowplaying-info>
  <nowplaying-info mountName="CKOIFMAAC" timestamp="1339771364" type="track">
    <property name="track_artist_name"><![CDATA[AMYLIE]]></property>
    <property name="cue_title"><![CDATA[LES FILLES]]></property>
  </nowplaying-info>
  <nowplaying-info mountName="CKOIFMAAC" timestamp="1339771149" type="track">
    <property name="track_artist_name"><![CDATA[MIA MARTINA]]></property>
    <property name="cue_title"><![CDATA[TOI ET MOI]]></property>
  </nowplaying-info>
</nowplaying-info-list>

我也尝试删除第一行以防 UTF-8 与 Windows 不兼容(看到一些关于此的帖子),但我仍然遇到同样的错误。我还尝试了 unix2dos,反之亦然,以防出现回车问题(嵌入在 xml 中的隐藏字符)。我似乎无法弄清楚出了什么问题。它就是这样一个 simole XML 文件。我可以使用 perl regex 在几分钟内解析它,但我需要在 Windows 上运行这个脚本,所以使用 vbscript。我使用相同的技术从其他来源解析 XML,没有任何问题。不幸的是,我无法修改 XML,它来自外部源。 我的 Windows Vista 家庭版和 Windows Server 2008 上都有完全相同的错误。到目前为止,我正在从命令行运行 vbscript 进行测试(即不在 ASP 中)。

提前致谢

山姆

最佳答案

xmlDoc.loadXML() 可以加载 XML 字符串。它无法检索 URL。

如果需要发出 HTTP 请求,请使用 XMLHTTPRequest 对象。

Function LoadXml(xmlurl)
  Dim xmlhttp

  Set xmlhttp = CreateObject("MSXML2.XMLHTTP")
  xmlhttp.Open "GET", xmlurl, false

  ' switch to manual error handling
  On Error Resume Next

  xmlhttp.Send
  If err.number <> 0 Then 
    WScript.Echo xmlhttp.parseError.Reason
    Err.Clear
  End If 

  ' switch back to automatic error handling 
  On Error Goto 0

  Set LoadXml = xmlhttp.ResponseXml
End Function

像这样使用

Set doc = LoadXml("http://your.url/here")

关于VBScript 和 loadXML : Invalid at the top level of the document. 如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11062074/

相关文章:

c# - 使用 vbscript 访问窗口的标题

javascript - Node.js XPath 示例?

xml - 使用 VBScripting 将节点添加到 xml 文件

xml - 使用 vbscript 创建 XML 元素时如何避免 xmlns 属性?

vbscript - Internet Explorer 自动化繁忙与就绪状态属性

javascript - 动态调用 JS 文件加载到经典的 ASP 应用程序中 - 我们的开发人员想要实现什么目标?

file - 如何使用 VisualBasicScript (.vbs) 运行文件

java - dom xml 解析器 java,相同的标签

javascript - 添加合格的 XML 元素不会继承命名空间 URI

xml - 在 VBScript 中为 Dummy 导航 XML 节点