Home arrow JavaScript arrow Page 3 - JavaScript Remote Scripting: Fetching Server Data with the DOM
JAVASCRIPT

JavaScript Remote Scripting: Fetching Server Data with the DOM


This article provides you with a handy method for making JavaScript-based requests to the server and serving XML documents, all without the need to use AJAX technology. If you’re inclined to work very close to W3C standards, this is an approach worth considering, since it allows the implementation of Web services through a cross-domain integration.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 11
October 12, 2005
TABLE OF CONTENTS:
  1. · JavaScript Remote Scripting: Fetching Server Data with the DOM
  2. · One step toward standardization: making http requests with the DOM
  3. · Setting up the server response: parsing XML in the server
  4. · Displaying XML data: defining the “createDataContainer()” and “displayData()” functions
  5. · Putting the pieces together: showing the complete script

print this article
SEARCH DEVARTICLES

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.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

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

Developer Shed Affiliates

 




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