JavaScript
  Home arrow JavaScript arrow Page 3 - JavaScript Remote Scripting: Processing XM...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

JavaScript Remote Scripting: Processing XML Files
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    2005-10-05

    Table of Contents:
  • JavaScript Remote Scripting: Processing XML Files
  • XML in the client: the basics of AJAX XML processing
  • Reading XML files with AJAX: defining the “sendRequest()” function
  • Checking the progress of a request: a quick look at the “stateChecker()” function
  • Displaying XML data: defining the “createDataContainer()” and “displayData()” functions
  • Putting the pieces together: listing the complete script

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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
        if(xmlobj!=null&&xmlobj.readyState!=0&&xmlobj.readyState!=4){
            xmlobj.abort();
        }
        try{
            // instantiate object for Mozilla, Nestcape, etc.
            xmlobj=new XMLHttpRequest();
        }
        catch(e){
            try{
                // instantiate object for Internet Explorer
                xmlobj=new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch(e){
                // Ajax is not supported by the browser
                xmlobj=null;
                return false;
            }
        }
        // assign state handler
        xmlobj.onreadystatechange=stateChecker;
        // open socket connection
        xmlobj.open('GET',doc,true);
        // send GET request
        xmlobj.send(null);
    }

    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.

    More JavaScript Articles
    More By Alejandro Gervasio


       · This second part of the series goes through the development of a JavaScript...
       · HiPlease can any one help me to manage a stick up footer on the website for IE...
     

    JAVASCRIPT ARTICLES

    - More on JavaScript Array Objects
    - Methods of the DOM Location Object
    - The DOM Location Object Properties
    - Handling Remote Files with JavaScript Click ...
    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...


     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT