Link to home
Start Free TrialLog in
Avatar of saturation
saturation

asked on

XMLDOM error parsing in ASP

I get the following error with the code below it on the Response.Write line.  What am I doing wrong?

Microsoft VBScript runtime error '800a01a8'
Object required: '[object]'


Dim xmlDocSet
Set xmlDocSet = Server.CreateObject("Microsoft.XMLDOM")
xmlDocSet.load(sRequest)
Response.Write xmlDocSet.documentElement.childNodes(0).attributes.getNamedItem("LISTING").nodeValue
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Verify that the xml is loading properly first:

Set xmlDocSet=Server.CreateObject("MSXML2.DOMDocument")
xmlDocSet.load sRequest

If Err.Number<>0 Then
 Response.Write "Error ocurred:" & Err.description
Else
   'do your processing here
   Response.Write xmlDocSet.documentElement.childNodes(0).getAttribute("LISTING")
End If

Reference:
https://www.experts-exchange.com/questions/20537562/XML-Dom-GetAttribute-in-ASP.html
Avatar of saturation
saturation

ASKER

I got

Microsoft VBScript runtime error '800a01a8'

Object required: 'documentElement'
where is the xml you are trying to load. If the xml is invalid, then you will get the error you just described.
Here's the XML:

<?xml version="1.0" encoding="UTF-8"?>
<RESULTS>
<LISTING>
<RANK>1</RANK>
<TITLE>My Site</TITLE>
<DESCRIPTION>Search Engine.</DESCRIPTION>
<SITEHOST>MySite</SITEHOST>
<LINK>http://www.mysite.com</LINK>
<ID1>LFPvAffVC0</ID1>
</LISTING>
</RESULTS>
Sorry, I meant

<?xml version="1.0" encoding="UTF-8"?>
<RESULTS>
<LISTING>
<RANK>1</RANK>
<TITLE>My Site</TITLE>
<DESCRIPTION>Search Engine.</DESCRIPTION>
<SITEHOST>MySite</SITEHOST>
<LINK>http://www.mysite.com</LINK>
</LISTING>
</RESULTS>
Ignore the hyperlink.   The XML is well-formed.

Set xmlDoc =Server.CreateObject("MSXML2.DOMDocument")

Dim xml

xml="<?xml version=""1.0"" encoding=""UTF-8""?><RESULTS><LISTING><RANK>1</RANK><TITLE>My Site</TITLE><DESCRIPTION>Search Engine.</DESCRIPTION><SITEHOST>MySite</SITEHOST><LINK>http://www.mysite.com</LINK></LISTING></RESULTS>"
xmlDoc.loadXML xml

'if you have the xml in a file, comment out the two lines above and uncomment the 
'two lines that follow instead and provide the right name for your file
'xml="file.xml"
'xmlDoc.load xml

xmlDoc.setProperty "SelectionLanguage", "XPath"
Set nodes = xmlDoc.documentElement.selectNodes("//RESULTS/LISTING")
For Each node in nodes
	'this will give you the content of the LISTING node
	'Response.write( node.text )

	'but if you need to access specific "sub-nodes", then use the selectSingleNode()
	'method on the node object
	Response.write( "Rank" & node.selectSingleNode("RANK").text )
	Response.write( "Title" & node.selectSingleNode("Title").text )
Next
Set nodes = Nothing
Set xmlDoc = Nothing

Open in new window

I get

msxml3.dll error '80004005'
Attempt to modify a read-only node.

The line of code that errors is

xmlDocSet.setProperty "SelectionLanguage", "XPath"

Thoughts?
are you loading a local file? If so, are you specifying the full path to the file?
I just load the XML into a variable on the page.   Should it be loaded into a local file?
>>Should it be loaded into a local file?
It's not necessary to do that. The example I posted loads the data from a variable and it works fine. The only problem with what I posted earlier is:
node.selectSingleNode("Title").text

Your xml node name is in upper case, so it should be:
node.selectSingleNode("TITLE").text

I was just trying to make sure the data is loading fine. Can you post your code?
Dim xmlDocSet
Set xmlDocSet=Server.CreateObject("MSXML2.DOMDocument")
xmlDocSet.load sRequest

xmlDocSet.setProperty "SelectionLanguage", "XPath"
Set nodes = xmlDocSet.documentElement.selectNodes("//RESULTS/LISTING")
For Each node in nodes
        'this will give you the content of the LISTING node
        'Response.write( node.text )
 
        'but if you need to access specific "sub-nodes", then use the selectSingleNode()
        'method on the node object
        Response.write( "Rank" & node.selectSingleNode("RANK").text )
        Response.write( "Title" & node.selectSingleNode("TITLE").text )
Next
Set nodes = Nothing
Set xmlDocSet = Nothing

>>I just load the XML into a variable on the page.
OK, the following contradict the statement above:
xmlDocSet.load sRequest

If sRequest contains the XML "string" (not a URL), then you should be using:
xmlDocSet.loadXML sRequest

If sRequest is the name of your xml file, then provide an absolute path:
xmlDocSet.load Server.Mappath(sRequest)
If I set to loadXML instead of load, I get

Microsoft VBScript runtime error '800a01a8'
Object required: '[object]'

Set nodes = xmlDocSet.documentElement.selectNodes("//RESULTS/LISTING")

is the line of the error.
try this:
<%
Dim sRequest

sRequest="<?xml version=""1.0"" encoding=""UTF-8""?><RESULTS><LISTING><RANK>1</RANK><TITLE>My Site</TITLE><DESCRIPTION>Search Engine.</DESCRIPTION><SITEHOST>MySite</SITEHOST><LINK>http://www.mysite.com</LINK></LISTING></RESULTS>"

Dim xmlDocSet
Set xmlDocSet=Server.CreateObject("MSXML2.DOMDocument")
xmlDocSet.loadXML sRequest

If Err.Number = 0 Then
	xmlDocSet.setProperty "SelectionLanguage", "XPath"
	Set nodes = xmlDocSet.documentElement.selectNodes("//RESULTS/LISTING")
	For Each node in nodes
	        'this will give you the content of the LISTING node
	        'Response.write( node.text )
	 
	        'but if you need to access specific "sub-nodes", then use the selectSingleNode()
	        'method on the node object
	        Response.write( "Rank" & node.selectSingleNode("RANK").text )
	        Response.write( "Title" & node.selectSingleNode("TITLE").text )
	Next
	Set nodes = Nothing
Else
	Response.Write Er.number & " " & Err.Description
End If
Set xmlDocSet = Nothing

%>

Open in new window

So bizarre, back to

Microsoft VBScript runtime error '800a01a8'
Object required: 'documentElement'

on line

Set nodes = xmlDocSet.documentElement.selectNodes("//RESULTS/LISTING")

I double-checked and the xml is fine.
what if you change:
MSXML2.DOMDocument

to:
Microsoft.XMLDOM
No luck...And just to test, I just created a simple file called "test1.xml" with the following, then the asp code is below...If I call test1.xml from a browser, it shows up in teh browser as xml.    Why would I keep getting

Microsoft VBScript runtime error '800a01a8'
Object required: 'documentElement'





<?xml version="1.0" encoding="UTF-8"?>
<RESULTS>
<LISTING>
<RANK>1</RANK>
</LISTING>
</RESULTS>



'ASP CODE

Dim xmlDocSet
'Set xmlDocSet=Server.CreateObject("MSXML2.DOMDocument")
Set xmlDocSet=Server.CreateObject("Microsoft.XMLDOM")
xmlDocSet.loadXML "test1.xml"
 
If Err.Number = 0 Then
        xmlDocSet.setProperty "SelectionLanguage", "XPath"
        Set nodes = xmlDocSet.documentElement.selectNodes("//RESULTS/LISTING")
        For Each node in nodes
                'this will give you the content of the LISTING node
                'Response.write( node.text )
         
                'but if you need to access specific "sub-nodes", then use the selectSingleNode()
                'method on the node object
                Response.write( "Rank" & node.selectSingleNode("RANK").text )
            '    Response.write( "Title" & node.selectSingleNode("TITLE").text )
        Next
        Set nodes = Nothing
Else
        Response.Write Er.number & "yes " & Err.Description
End If
Set xmlDocSet = Nothing


OK, let me say it again.
If you are using a FILE, you do NOT use .loadXML(); you use just ".load()"

FILE => .load()
STRING => .loadXML()

So you need
xmlDocSet.load Server.Mappath("test1.xml")

Ok, now we're getting places...When I do it with that method, it parses it.   I'm guessing that means that my xml in memory ISN'T, after all, well-formed.   If you look at my xml output to the page from memory, it appears to be fine, so I'm not sure...Can someone take a look at thepetbox.com/test.asp and tell me what might be the problem?
It's working fine. The reason it looks "cryptic" is because your xml contains encoded urls. You need to decode them.

Unfortunately for you, there a function named Server.urlEncode() BUT there is NO server.urlDecode().

Fortunately for you, someone already did that:
http://flangy.com/dev/asp/urldecode.html

Copy both of those functions to your asp script and then instead of writing the node's text directly:
  Response.write( "Rank" & node.selectSingleNode("LINK").text )

you will need to pass it to the decoding function first and print the result of the decoding function:
  Response.write( "Rank" & URLDecode(node.selectSingleNode("LINK").text)  )
But see, it writes it to the page fine when I don't parse through it, which is what I'm doing there...If I use the XMLDOM object...But when I put that code back in and try to just write each node out to the page, it errors on line

        Set nodes = xmlDocSet.documentElement.selectNodes("//RESULTS/LISTING")

Check the page again and you'll see what I mean...I just put the code back.
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Wow...I just realized that I was putting the request variable into the load method instead of the actualy XML.   Hielo, you have been a HUGE help!   Thank you so much!