简体   繁体   English

如何使用vbscript和经典asp在xml中选择SingleNode?

[英]How do I selectSingleNode in xml with vbscript and classic asp?

From here : xml: 这里 :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>

I created the xml file put it in the same directory as the classic asp file: 我创建了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)

%>

But I get this: 但我明白了:

Microsoft VBScript runtime error '800a01a8' Microsoft VBScript运行时错误“800a01a8”

Object required: 'objXMLDoc.documentElement' 需要的对象:'objXMLDoc.documentElement'

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

What am I doing wrong? 我究竟做错了什么? They get the element. 他们得到了元素。 I get the error. 我收到了错误。 Thanks. 谢谢。

Edit: I put this in: 编辑:我把它放进去:

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

and got: 得到了:

-2146697210ERROR CODE -2146697210ERROR CODE

System error: -2146697210. 系统错误:-2146697210。 REASON CODE 原因代码

0LINE CODE tried from below: 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)

EDIT: 编辑:

I also changed the xml file to a URL of a working XML return (bing maps) and it worked. 我还将xml文件更改为工作XML返回(bing maps)的URL,并且它有效。 So I guess it's the file. 所以我猜这是文件。 Thanks. 谢谢。

I think your xml document is not loading. 我认为你的xml文档没有加载。 The load() method returns a bool to indicate whether the file has loaded correctly, so you can check load()方法返回一个bool来指示文件是否已正确加载,因此您可以检查

if not objXMLDoc.load("vocabulary.xml") then
    ' report loading error
end if

The parseError also property has a srcText property which you can use to determine where a parse issue has occurred in the file. parseError属性还有一个srcText属性,您可以使用该属性来确定文件中发生解析问题的位置。

It's also a good idea to check that the file exists on the path you're using. 检查文件是否存在于您正在使用的路径上也是一个好主意。 Use Server.MapPath() and a Scripting.FileSystemObject to do this: 使用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

Additionally, I recommend using a later version of the XML library, MSXML2.DomDocument 此外,我建议使用更高版本的XML库MSXML2.DomDocument

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM