Home arrow JavaScript arrow Page 3 - JavaScript Remote Scripting: Processing XML Files

JavaScript Remote Scripting: Processing XML Files

In this article, you will learn how to use AJAX for reading and manipulating simple XML files, in order to implement a basic Web service. Specifically, you will learn how to develop a JavaScript application that fetches data from an XML file. By the end of this article, you will know the basics for using client-side parsed XML in your own applications.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 18
October 05, 2005
  1. · JavaScript Remote Scripting: Processing XML Files
  2. · XML in the client: the basics of AJAX XML processing
  3. · Reading XML files with AJAX: defining the “sendRequest()” function
  4. · Checking the progress of a request: a quick look at the “stateChecker()” function
  5. · Displaying XML data: defining the “createDataContainer()” and “displayData()” functions
  6. · Putting the pieces together: listing the complete script

print this article

JavaScript Remote Scripting: Processing XML Files - Reading XML files with AJAX: defining the “sendRequest()” function
(Page 3 of 6 )

If you’ve read the first part of this series, the “sendRequest()” function should be already pretty familiar. Essentially, it performs two well-delimited tasks: the first one consists of instantiating cross-browser XMLHttpRequest objects, while the second one is responsible for sending the proper http request and fetching the contents of the file passed as a function argument. Here is its definition:

function sendRequest(doc){
    // check for existing requests
        // instantiate object for Mozilla, Nestcape, etc.
        xmlobj=new XMLHttpRequest();
            // instantiate object for Internet Explorer
            xmlobj=new ActiveXObject('Microsoft.XMLHTTP');
            // Ajax is not supported by the browser
            return false;
    // assign state handler
    // open socket connection
    // send GET request

In simple terms, the above defined function is flexible enough to handle multiple data formats, so it’s not only restricted to reading XML files. Notice that the “doc” argument could be any file feasible for being processed through a GET request, a feature that turns the function into a very versatile piece of code.

Also, by studying the snippet a little bit deeper, it becomes clear that it allows for the fetching of content that is dynamically generated. Instead of passing in only the name of the file to be fetched, it’s possible to append some variables to the querystring, because it’s done with regular links. For instance, if the requested file is “script_file.php”, it’s extremely easy to add a few parameters for processing on the server, in the form “script_file.php?param1=1&param2=2”. I guess this is enough of an explanation for you to start tweaking the code to pull out dynamic content.

Having explained how the “sendRequest()” function does its business, it’s time to take a look at the next function “stateChecker()”, which as you’ll see in a moment, controls the flow of the whole program.

blog comments powered by Disqus

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials