我如何在带有vbscript和经典ASP的xml中选择selectSingleNode?

joh*_*nny 3 xml vbscript asp-classic

这里:xml:

<Vocabulary> 
   <Word type="noun" level="1"> 
      <English>cat</English> 
      <Spanish>gato</Spanish> 
   </Word> 
   <Word type="verb" level="1"> 
      <English>speak</English> 
      <Spanish>hablar</Spanish> 
   </Word> 
   <Word type="adj" level="1"> 
      <English>big</English> 
      <Spanish>grande</Spanish> 
   </Word> 
</Vocabulary>
Run Code Online (Sandbox Code Playgroud)

我创建了一个xml文件,将其与经典asp文件放在同一目录中:

    <%

    Set objXMLDoc = CreateObject("Microsoft.XMLDOM") 
    objXMLDoc.async = False 
    objXMLDoc.load("vocabulary.xml") 

    Set Node = objXMLDoc.documentElement.selectSingleNode("Word/Spanish")
    document.write(Node.text)

%>
Run Code Online (Sandbox Code Playgroud)

但是我得到这个:

Microsoft VBScript运行时错误'800a01a8'

所需对象:“ objXMLDoc.documentElement”

/so-rms/reports/xmltest.asp,第7行

我究竟做错了什么?他们得到了元素。我得到了错误。谢谢。

编辑:我把它放在:

If objXMLDoc.parseError.errorCode <> 0 Then
    response.write objXMLDoc.parseError.errorCode & "ERROR CODE </br>"
    response.write  objXMLDoc.parseError.reason & "REASON CODE </br>"
    response.write  objXMLDoc.parseError.line & "LINE CODE </br>"
End If
Run Code Online (Sandbox Code Playgroud)

并得到:

-2146697210错误代码

系统错误:-2146697210。原因码

0LINE CODE从下面尝试过:

dim path: path = Server.MapPath("vocabulary.xml")
dim fso: set fso = CreateObject("Scripting.FileSystemObject")
if not fso.FileExists(path) then
    Response.Write "path '" & path & "' not found"
end if
Set objXMLDoc = CreateObject("MSXML2.DOMDocument.3.0") 
objXMLDoc.async = False 

if not objXMLDoc.load("vocabulary.xml") then
    ' report loading error
     response.write "error"
end if
'objXMLDoc.load("vocabulary.xml") 
If objXMLDoc.parseError.errorCode <> 0 Then
    response.write objXMLDoc.parseError.errorCode & "ERROR CODE </br>"
    response.write  objXMLDoc.parseError.reason & "REASON CODE </br>"
    response.write  objXMLDoc.parseError.line & "LINE CODE </br>"
End If
Set Node = objXMLDoc.documentElement.selectSingleNode("Word/Spanish")
document.write(Node.text)
Run Code Online (Sandbox Code Playgroud)

编辑:

我也将xml文件更改为工作的XML返回(bing映射)的URL,它可以工作。所以我想这是文件。谢谢。

小智 5

我认为您的xml文档未加载。该load()方法返回,bool以指示文件是否已正确加载,因此您可以检查

if not objXMLDoc.load("vocabulary.xml") then
    ' report loading error
end if
Run Code Online (Sandbox Code Playgroud)

parseError还酒店有srcText,您可以使用来确定一个解析的问题已经出现在该文件中属性。

检查文件是否在您使用的路径上也是一个好主意。使用Server.MapPath()Scripting.FileSystemObject执行此操作:

dim path: path = Server.MapPath("vocabulary.xml")
dim fso: set fso = CreateObject("Scripting.FileSystemObject")
if not fso.FileExists(path) then
    Response.Write "path '" & path & "' not found"
end if
Run Code Online (Sandbox Code Playgroud)

另外,我建议使用更高版本的XML库, MSXML2.DomDocument