开发者

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.

  1. The error is thrown from objXMLDoc.load and not objXMLDoc.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. Try Response.write objXMLDoc.text to see if you can get anything, it should display "Some label" too.

  2. 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜