tags:

views:

69

answers:

2

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="utf-8" ?>
    <labels>
        <label>Some label</label>
    </labels>
+2  A: 

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
mjv
"labels" is the document element, so documentElement.selectSingleNode call should indeed return the one label node.
wsanville
I needed to use a full path with selectSingleNode for it to work.
chobo
+2  A: 

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
o.k.w
I also tried switching msxml versions. I'm not sure if that helped or not.
chobo