Total members 11895 |It is currently Sat Dec 21, 2024 5:20 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





My first Ajax application ,(Read XML file from AJAX) this application is so simple.The AJAX application read an XML file from the server and load some data from it.

Here is the html file :
html code
<html>
<script type="text/javascript">
//read data from xml file
function getXMLdata()
{
var request=null;

//Depends on the your browser
if(window.XMLHttpRequest)
{
//Mozila FireFox
request=new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
//Internet Explore
request = new ActiveXObject("Microsoft.XMLHTTP");
}

if(request)
{
//The autocalled funtion when the status changed
request.onreadystatechange = function() {

//Status cases of the readState varrible
if(request.readyState==0)
document.getElementById('status').innerHTML='Uninitialized';
else if(request.readyState==1)
document.getElementById('status').innerHTML='Loading...';
else if(request.readyState==2)
document.getElementById('status').innerHTML='Loaded';
else if(request.readyState==3)
document.getElementById('status').innerHTML='Interactive';
else if(request.readyState==4)
{
document.getElementById('status').innerHTML='Completed';
var xml_response = request.responseXML;
xml_response.loadXML(request.responseText);
var myname=xml_response.getElementsByTagName('name');
document.getElementById('Txtdata').value=myname[0].childNodes[0].nodeValue;
}

}

request.open("GET","Data.xml");
//send the request
request.send(null);

}
else
//not request opened
document.getElementById('Txtdata').value='Nothing';
}

</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- used for as a meta informaton to the browser -->
<title>Untitled Document</title>
</head>

<span id = "status"></span ><br>
<input type="text" id="Txtdata" />
<input type="button" value="Load" onclick="getXMLdata();" />
<body>
</body>
</html>


And here is XML file :
xml code
<?xml version = "1.0" ?>
<Boss>
<name>AAA</name>
<age>43
</age>
<sex>Male
</sex>
<name>ALI</name>
<age>43
</age>
<sex>Male
</sex>
</Boss>


In the beginning i faced an problem when i tried to show the data. Every time i had to got " undefined " instead of the real data.The problem was in the following part of the code.
javascript code
document.getElementById('Txtdata').value=myname[0].childNodes[0].nodeValue;

You have to take care where is your element position in the XML file and which node you want to get data from inside the XML file.



_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : My First Ajax application
 Ajax Source code to Suggest application with JSP Server side     -  
 Connecting Java Application to C++ Application from Code     -  
 What is AJAX, How to start AJAX?     -  
 First Application in c#     -  
 Mobile Application     -  
 want to develop an application of my own     -  
 I need chat application using php     -  
 About Mobile application     -  
 Help Sending e-mail from VB.Net application     -  
 Integrating .NET application in Java     -  



Topic Tags

AJAX XML
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com