XML and ASP: Retrieving and Parsing a Remote File

I am building a site on Windows Server with ASP enabled. I need to get the XML document from another server and return the value in this document. The xml file is small - only one node with a text value. I just need to return this text value. I had never worked with ASP before, and Googling around led me to some code examples, but nothing that works so far. Here is what I have that gives me 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 know my XML for sure. Only one node with a value. Based on your comments, I edited my asp code for:

 <% 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 %> 

That still returns 500. How can I debug ASP? Is there a way to enable detailed error reporting?

+4
source share
6 answers

I wrote this function:

 <% Option Explicit Response.Buffer = True Dim xml Set xml = Server.CreateObject("Microsoft.XMLDOM") xml.async = False xml.setProperty "ServerHTTPRequest", True xml.Load ("http://www.midominio.com/nombre.xml") Dim title, heading, paragraph, testHTML title = xml.documentElement.childNodes(0).text heading = xml.documentElement.childNodes(1).text paragraph = xml.documentElement.childNodes(2).text testHTML = xml.documentElement.childNodes(3).text Set xml = Nothing %> <html> <head> <title><%= title %></title> </head> <body> <h3 align="center"><%= heading %></h3> <p align="center"><% = paragraph %></p> <div align="center"><%= testHTML %></div> </body> </html> 
+3
source

Assuming your Xml is actually: -

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

Try using: -

 Dim value value = objXML.documentElement.Text 

By the way

When you call methods from which you do not return a value, you do not need parentheses: -

 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

+2
source

As Pete Duncanson said, the first thing to try is to turn off Show Friendly Error Messages.

If you still get 500 errors, they probably come from IIS (you can probably tell by looking at them). I have provided guidance for including error messages in IIS7 here if you need it.

+1
source

Change line 4 of your source snippet to

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

and line 14 is

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

and you should be fine (assuming your xml is described in the AnthonyWJones description).

Your original // xml / response will receive text from a document that looks like this:

 <?xml version="1.0" ?> <xml> <response>hello</response> </xml> 
+1
source

ASP debugging is not as pleasant as you might get used to. This should help:

  • If using IE ensures that you have disabled "Show friendly error messages" in the options
  • Use Response.Write to track how far you get the code.

Perhaps you have a 500 error handler page on the server you are using (assuming that you are not working locally). In this case, you will have to change the 500-page page if you can give the real error in such detail (see http://www.w3schools.com/ASP/asp_ref_error.asp) , If you develop locally, although you usually get all the juicy details.

0
source

ASP classic debugging is a disgusting topic that millions of foreign brain cells have sacrificed over the years. Even with tools designed to develop and / or support classic ASP, allowing debugging can be difficult.

If your efforts are a relatively small one-time thing, as your question suggests, then it probably doesn’t make sense to spend a lot of time setting up and setting up an advanced ASP / script debugging environment. Instead, as per Pete Duncanson’s answer, just insert a few Response.Write statements into your script and find out where and why it doesn't work the old fashioned way. However, one thing Pete did not point out is that you need to enable the VBScript error handler (actually, the swallower error) to avoid throwing an unhandled exception, resulting in IIS serving 500 people.

I set up 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 computer as the ASP page, and contained the XML found in AnthonyWJones answer. (By the way, 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 all 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 will not be fine, otherwise you will not be here, and in this case you should see that the error data appears at some point after one of the Response.Write values. Here is more information about the VBScript Err object.

Good luck

0
source

All Articles