Why am I constantly getting object errors?
I have cut and paste this example from http://devguru.org/Technologies/xmldom/quickref/node_selectSingleNode.html
and I can't get it to work.
I keep getting object errors like this:
Microsoft VBScript runtime (0x800A01A8) Object required
This is the code and xml file I am using
<%
option explicit
Dim objXMLDoc
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load(Server.MapPath("vocabulary.xml"))
Dim Node
Set Node = objXMLDoc.documentElement.selectSingleNode("label")
Response.write Node.text
%>
xml file
<?xml version="1.0" encoding="开发者_StackOverflow社区utf-8" ?>
<labels>
<label>Some label</label>
</labels>
The error mentioned is probably at the level of the last line. Assuming all other calls to the XMLDOM object worked smoothly, selectSingleNode would return null, since "label" as a path would not be found.
Try with
Set Node = objXMLDoc.documentElement.selectSingleNode("labels/label")
instead. Alternatively, and this is a good practice with this type of DOM logic, you could test for successful return from selectSingleNode
Set Node = objXMLDoc.documentElement.selectSingleNode("label")
If Node = Nothing
Ehen
Response.Write "Not found..."
Else
Response.Write Node.text
I've tried your codes and it works. So there are 2 possible reasons that I can think of.
The error is thrown from
objXMLDoc.load
and notobjXMLDoc.selectSingleNode
which means the XML file is not found (or permission is denied?). Check that the file path is indeed valid and can be accessed. TryResponse.write objXMLDoc.text
to see if you can get anything, it should display "Some label" too.I'm just guessing but it could due to a different version of the "MSXML" library
If it's not reason 1, you might want to try the following code (from MSDN reference):
objXMLDoc.setProperty "SelectionLanguage", "XPath" 'add this line
Dim Node
Set Node = objXMLDoc.documentElement.selectSingleNode("//label") 'use //label
Response.write Node.text
精彩评论