BeaverBlogg

April 9, 2007

XML, XMLDom and ASP, Part 2

Filed under: ASP, Articles, XML — Karl @ 10:34 pm

This is the second article in my series about XML, XMLDom and ASP. For those who missed the first it’s available here

In my first article I showed how to parse an XML document using XMLDom and displaying the data. In this second tutorial I’ll show two different ways to retrieve an XML document from a remote server using ASP and ServerXMLHTTP.

XML is a great way to share information. However, if you want to be able to share it people will need to retrieve the data from you somehow (or you from them). For this purpose we have an object called XMLHTTP or, if used on a server, ServerXMLHTTP. Since both XMLDom and XMLHTTP are part of MSXML2 they can cooperate very nice. XMLDom can by auto use XMLHTTP to retrieve information and if you use XMLHTTP it can by auto return a XMLDom object with the retrieved XML data.

The easiest way to retrieve XML from an external site is to let the XMLDom handle it all. This requires the least amount of code and I would guess that it’s also a tiny bit more effective resource wise. Here is how to do it:

This code is really simple so I’ll describe it all in one chunk. First we declare the variable and create the xmlDom object. This is precisely as we did in the first article so far. Next we call the setProperty method to specify that it should use the “ServerHTTPRequest” feature. By specifying this we are able to pass an URL to the load method which we do on the next line. In this example it will load my RSS feed into the xmlDom.

This might seem like a great solution since it’s so simple, however it has some drawbacks. First of all, if the URL is unavaible, it will throw an error. Aswell as if the loaded data isn’t wellformed xml, it will throw an error. So to be sure that this code doesn’t throw an nasty error on your visitors you would have to extend it with some error handling.

There we have it, some basic error handling to see if it loaded or not. I expect that people know at least some about error handling in ASP. If not, then write a comment about it and I make take it up a bit more in an later article.

The other way to go to retrieve external information is to manually create the ServerXMLHTTP object and making it browse to the URL and return it’s contents. This requires a bit more code, but it also has some other nice features. For example, it can post data to the remote server, it can retrieve other data then just xml like a binary file or a simple text document. I will start by creating the same example as I did previous in this article using ServerXMLHTTP.

This made for a whole extra line to the code if compared to the original earlier in this post. This one isn’t much harder to understand then the previous one. We declare the variables, we create the ServerXMLHTTP object and then we call the Open method of the xmlHttp object. This method takes two arguments, first one is a string which describes the HTTP method to use. GET is fine unless you need to post data which you mostly won’t do. The second argument is the URL to retrieve the data from and again we use my RSS feed. After that we call the Send method which will trigger the object to browse to our URL and fetch the data. The Send method takes one optional argument which is the data to send to the URL. When using GET as method this has no real function. Last in the script we retrieve an xmlDom from the responseXML property of xmlHttp.

That wasn’t to bad was it? Though it isn’t ever as simple as it seems. If you are browsing to a url that doesn’t return “text/xml” as content type the responseXML property will be empty so we will need to add some error handling to that as well. Also, if you take the URL as a dynamic parameter and it doesn’t contain a valid URL or the URL is unavaible it will throw an error. So lets add the same as in the previous example here aswell and add some stuff to it.

I know, it’s a mess. But it covers all problems that can arise and provides specific error messages for the different ones. I have to admit, I never go this far in error handling but I still want to show what that can be done and what to look for.

After calling the Send method it might be an good idea to check the error object. If something failed miserably it probably was here.

After that it can always be an good idea to have a look at the status property to see what HTTP response code it returned. It it wasn’t 200 (HTTP 200 OK) we don’t want the data. Other usual statuscodes are 404 (File not found) and 500 (Internal server error) so those who want it really nice can check for those aswell and output it to the user.

Last we check if there is a responseXML object avaible. If it isn’t, then the server returned an invalid response (and might be caught already but not for sure).

There we have it, two different approaches to retrieving XML from an external source. There is however one more thing I would like to talk about. In this article I’ve used the ServerXMLHTTP object. When asking my good friend Google about retrieving external data using ASP I found several articles telling the readers to create the “Microsoft.XMLHTTP” object or “MSXML2.XMLHTTP.4.0″. This will work on your ASP page, but it’s not a very good idea to use. They were created for use in a client environment, not to be executed by a server. They rely on winsock (or some other client side communication layer, can’t remember exactly which, sorry) instead of winhttp which is used for server applications. Being built as clientside objects they also use the current proxy settings from Internet Explorer which might not always be desirable. I will get back to these objects in a later article when I start to talk about Ajax development which works with these objects using clientside JavaScript.

I haven’t decided yet what to write about in Part 3 to be honest. It will be XML for sure, but I’m not sure if I will go for simple XSL stylesheets or creating new documents using xmlDom only. Tune in for my next article and we’ll see what it contains :) .

2 Comments »

  1. I’m facing a the following erro “The system cannot locate the resource specified” .
    I’m using xml and asp. plz helpd me to come out this problem.:(

    Comment by Sapna Singh — July 28, 2008 @ 6:01 pm

  2. I have read both Part 1 and Part 2 of the article. This has been very useful. I think you should consider writing Part 3 as the missing part is creating/updating/deleting records in XML file.

    Comment by Sylvester — October 1, 2008 @ 2:21 am

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress