开发者

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

开发者 https://www.devze.com 2023-04-01 13:48 出处:网络
From here: xml: <Vocabulary> <Word type=\"noun\" level=\"1\"> <English>cat</English>

From here: xml:

<Vocabulary> 
   <Word type="noun" level="1"> 
      <English>cat</English> 
      <Spanish>gato</Spanish> 
   <开发者_如何学Python/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:

    <%

    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'

Object required: 'objXMLDoc.documentElement'

/so-rms/reports/xmltest.asp, line 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

System error: -2146697210. REASON CODE

0LINE CODE tried from below:

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. So I guess it's the file. Thanks.


I think your xml document is not loading. The load() method returns a bool to indicate whether the file has loaded correctly, so you can check

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.

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:

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

0

精彩评论

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