JavaScript
  Home arrow JavaScript arrow Page 3 - JavaScript Remote Scripting: Fetching Serv...
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: Fetching Server Data with the DOM
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 7
    2005-10-12

    Table of Contents:
  • JavaScript Remote Scripting: Fetching Server Data with the DOM
  • One step toward standardization: making http requests with the DOM
  • Setting up the server response: parsing XML in the server
  • Displaying XML data: defining the “createDataContainer()” and “displayData()” functions
  • Putting the pieces together: showing 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: Fetching Server Data with the DOM - Setting up the server response: parsing XML in the server


    (Page 3 of 5 )

    Having defined the core “sendRequest()” function and explained how it operates, it’s time to have a look at the program code that is executed when the “script_file.php” file is fetched. First, I’ll code a basic XML parser in PHP, in order to process the XML file used for defining the corresponding headlines. Then, after the XML nodes have been stored in a PHP array, the data will be copied to a new JavaScript array for processing by the originating script.

    Let’s start out with coding the server part, by defining a simple XML parser, in conjunction with the proper http header for serving the document as native JavaScript. The PHP snippet looks like this:

    header('Content-Type: text/javascript');
    echo 'var URL=new Array();var TITLE=new Array();';
    // read XML file
    $content=file_get_contents($_GET['file']);
    // store XML file contents in array
    $xml_parser=xml_parser_create();
    xml_parse_into_struct($xml_parser,$content,$vals);
    xml_parser_free($xml_parser);
    // store titles in TITLE array & urls in URL array
    foreach($vals as $key=>$value){
        if($value[tag]=='URL'||$value[tag]=='TITLE'){
            echo $value[tag].'['.$value[tag].'.length]=\''.$value
    [value].'\';';
        }
    }

    As you can appreciate, the PHP script is pretty straightforward. Basically, what it does is parse the XML file, passed in as a querystring argument, and then store its contents in the PHP “$vals” array. The following fragment of code performs these operations:

    $content=file_get_contents($_GET['file']);
    // store XML file contents in array
    $xml_parser=xml_parser_create();
    xml_parse_into_struct($xml_parser,$content,$vals);
    xml_parser_free($xml_parser);

    Now, before I move on to explaining the rest of PHP code, let’s pause for a moment and remember how the “news.xml” file looks, so the remaining program lines will be easier to understand:

    <?xml version="1.0" encoding="iso-8859-1"?>
    <news>
    <message>
    <title>Learn advanced concepts about AJAX and Remote Scripting.
    Visit DevArticles.com now!</title>
    <url>http://www.devarticles.com</url>
    </message>
    <message>
    <title>PHP 5.1 now presents new key improvements such as PDO (PHP
    Data Objects)</title>
    <url>http://www.php.net</url>
    </message>
    <message>
    <title>Google to continue extending its GoogleMaps API</title>
    <url>http://www.google.com/apis/maps/documentation/</url>
    </message>
    <message>
    <title>MySQL releases Open Source Database Tools for Macintosh OS
    X</title>
    <url>http://www.mysql.com</url>
    </message>
    <message>
    <title>Release of Flash Player 8 allows to develop richer
    applications and communications</title>
    <url>http://www.macromedia.com</url>
    </message>
    <message>
    <title>PHP introduces more improvements in regards to SOAP,
    streams and SPL</title>
    <url>http://www.php.net</url>
    </message>
    </news>

    With the definition of the headlines XML file well under way, let’s continue  explaining the remaining code.

    Now that all the XML nodes have been stored in a PHP variable, they need to be copied to a couple of JavaScript arrays. The first “TITLE” array will store the contents of all the <title> XML nodes, while the second one, that is  “URL”, will house the values of all the <url> nodes. Despite the complex-sounding description, a simple loop is all I need to get the server-to-client copy done. Here is the responsible code:

    foreach($vals as $key=>$value){
        if($value[tag]=='URL'||$value[tag]=='TITLE'){
            echo $value[tag].'['.$value[tag].'.length]=\''.$value[value].'\';';
        }
    }

    At this point, all the relevant XML nodes have been copied to the above mentioned JavaScript arrays. Therefore, the next thing to be done is define the JavaScript functions that will display the appropriate headlines.

    More JavaScript Articles
    More By Alejandro Gervasio


       · Over this article, find out how to make silent http requests, by using only the DOM....
       · Hi, I thought it is a good article to understand how to use javascript to...
       · Hello,I'm glad to know this article has been useful to you. Now, comming to your...
       · Hi, Alejandro,I found your article really thorough and easy-to-understand. Thank...
       · Hi friend,Thanks a lot for the kind comments on my JavaScript article. I found...
       · Hi,This article is very helpful for me.But I am facing difficulties in running...
       · Thank you for commenting on my JavaScript article, and I’m glad it’s been useful to...
       · Hi,Thanks once again for the help.I have all the applications installed in my...
       · Thank you for commenting on my JavaScript article. Here are some basic steps to...
     

    JAVASCRIPT ARTICLES

    - 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...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






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