views:

1175

answers:

5

I'm building a site on a Windows Server with ASP enabled. I need to retrieve an XML document from another server and return a value in that document. The xml file is small - only one node with a text value. I just need to return that text value. I've never worked with ASP before, and Googling around has led me to some code examples, but nothing that works so far. Here's what I've got, which gives me a 500:

<%
Dim URL, objXML
URL = "http://someserver.com/xml"
Set objXML = Server.CreateObject("MSXML2.DOMDocument.4.0")
objXML.setProperty "ServerHTTPRequest", True
objXML.async =  False
objXML.Load(URL)

If objXML.parseError.errorCode <> 0  Then
    Response.Write(objXML.parseError.reason)
    Response.Write(objXML.parseError.errorCode)
End If

Set oRoot = objXML.selectSingleNode("//xml/response")
var = oRoot.text

set objXML = nothing
%>

<%= var %>

===========

Update:

Yes, you're exactly correct about my XML. Just one node with a value. Based on your comments, I edited my asp code to:

<%

Dim URL, objXML, value
URL = "http://someserver.com/xml"
Set objXML = Server.CreateObject("MSXML2.DOMDocument.6.0")
objXML.setProperty "ServerHTTPRequest", True
objXML.async =  False
objXML.Load URL

Response.Write objXML.parseError.reason

value = objXML.documentElement.Text

set objXML = nothing

%>

<%= value %>

Which is still returning a 500. How do I go about debugging ASP? Is there some way to turn on detailed error reporting?

A: 

Assuming your Xml is in fact:-

<?xml version="1.0" ?>
<response>The value</response>

Try using:-

Dim value
value = objXML.documentElement.Text

BTW,

When you call methods from which you are not returning a value you do not need the brackets:-

objXML.Load Url

Response.Write objXML.parseError.reason

Also if this is your server, install MSXML6 and use MSXML2.DOMDocument.6.0. IF this is not your server use MSXML3.DOMDocument.3.0

AnthonyWJones
Thanks for your help. I added some more info about my situation - which is still not working right.
NatalieMac
The brackets comment is only a matter of style though Anthony. Personnally I like to always bracket my functions, my if statements, etc. I don't need to but to me it makes it more readable and reduces the number of "Doh!" errors I make :)
Pete Duncanson
@Pete: This is more than just style. The only reason the use of ( ) in the original code didn't result in a syntax error was because only one parameter is being passed. Passing 2 or more using ( ) without retrieving a result would fail. Hence it would make VBScript code more consistent by avoiding using ( ) in this way altogether. Additionally code using a ByRef parameter would break with ( ) since it turns the parameter into an expression. Finally I prefer to make VB code __Look__ like VB code rather than try to make it similar to other language styles, this helps to highlight bugs.
AnthonyWJones
A: 

Debugging ASP is not as pleasant as you might be used to. This should help though:

  • If using IE ensure you have unticked "Show friendly error messages" in the options
  • Use Response.Write's to track just how far you are getting through your code.

It could be that you have a 500 error handler page on the server you are using (assuming you are not running local). In which case you will have to modify the 500 page if you can so it gives you more details of the real error (see http://www.w3schools.com/ASP/asp_ref_error.asp). If you develop locally though you tend to get all the juicy details.

Pete Duncanson
A: 

As Pete Duncanson said, the first thing to try is to untick "Show friendly error messages".

If you are still getting 500 errors they are probably coming from IIS (you can probably tell by looking at them). I have put up a guide for enabling error messages on IIS7 here if you need that.

mike nelson
A: 

Classic ASP debugging is a nasty topic to which millions of otherwise fine brain cells have been sacrificed over the years. Even with tools intended for developing and/or supporting classic ASP, enabling debugging can be tricky.

If your effort is a relatively small one-time thing, as your question sort of suggests, then it probably doesn't make sense to spend a lot of time setting up and configuring an advanced ASP/script debugging environment. Instead, as per Pete Duncanson's answer, simply inject some Response.Write statements into your script and figure out where and why it is failing the old fashioned way. However, the one thing Pete didn't point out is that you'll need to turn on a VBScript error handler (error swallower, actually) in order to avoid tossing an unhandled exception, resulting in IIS serving you a 500.

I setup and ran the following code and it worked fine (i.e., no errors). The XML URL pointed to a simple file in the same virtual directory on the local machine as the ASP page and it contained the XML found in AnthonyWJones's answer. (Btw, I have no idea how you got your VBScript so well formatted in the original question, so my copy looks pretty bad.)

<%
On Error Resume Next  ' prevent tossing unhandled exception
Dim URL, objXML, value
URL = "http://someserver.com/xml"
Set objXML = Server.CreateObject("MSXML2.DOMDocument.6.0")
Response.Write "after CreateObject: " & Err.Description & "<br>"
objXML.setProperty "ServerHTTPRequest", True
Response.Write "after setProperty: " & Err.Description & "<br>"
objXML.async =  False
Response.Write "after async: " & Err.Description & "<br>"
objXML.Load URL
Response.Write "after Load: " & Err.Description & "<br>"

Response.Write objXML.parseError.reason
Response.Write "after write of parseError.reason: " & Err.Description & "<br>"

value = objXML.documentElement.Text
Response.Write "after setting value: " & Err.Description & "<br>"

set objXML = nothing

%>

<%= value %>

Open this in IE or Firefox and if everything goes well you should see this:

after CreateObject: 
after setProperty: 
after async: 
after Load: 
after write of parseError.reason: 
after setting value: 
The value

Of course, everything isn't going to go well, otherwise you wouldn't be here, in which case you should see the error details appear at some point following one of the Response.Write values. Here's some additional information about the VBScript Err object.

Good luck!

ewbi
A: 

Change line 4 of your original snippet to

Set objXML = Server.CreateObject("MSXML2.DOMDocument.6.0")

and line 14 to

Set oRoot = objXML.selectSingleNode("//response")

and you should be fine (assuming your xml is as AnthonyWJones describes).

Your original //xml/response would get the text from a document that looked like this

<?xml version="1.0" ?>
<xml>
    <response>hello</response>
</xml>
jammus