Naveen P.N

Just another Blog to play with programming

AJAX andXML

leave a comment »

Hi before be telling about AJAX and XML Let be tell u what is XML .XML stands for Extensible Markup Language which is an International standards of data exchange .Briefly when a server sends any data to client it actually the XML data coming from server to client and the browser which acts as an interpreter interprets all the XML tags into human readable format which is XSLT.

 

AJAX-XML.html

 

<html>

<head>

<title>Using Ajax and XML</title>

<script language = “javascript”>

function getGuest()

{

var xhr;

if (window.ActiveXObject)

      {

            xhr = new ActiveXObject(“Microsoft.XMLHTTP”);

      }

      else if (window.XMLHttpRequest)

      {

            xhr = new XMLHttpRequest();

            xhr.overrideMimeType(“text/xml”);

      }

 

            xhr.onreadystatechange = function()

            {

                  if (xhr.readyState == 4 && xhr.status == 200)

                  {

                  var xmlDocument = xhr.responseXML;

                       

                        removeWhitespace(xmlDocument);

                       

                  displayGuest(xmlDocument);

                  }

            }

      xhr.open(“GET”, “students.xml”, true);

xhr.send(null);

}

function displayGuest (xmldoc)

{

var eventsNode, eventNode, peopleNode;

var firstNameNode, lastNameNode, displayText;

//This goes to inventory Node

eventsNode = xmldoc.documentElement;

eventNode = eventsNode.firstChild;<!–here the actuall code lies here the firstchild will refer to computer science –>

peopleNode = eventNode.firstChild;<!–Same as Above–>

personNode = peopleNode.firstChild;

alert(personNode.firstChild.nodeValue);

displayText = “The Luckypersonis ” + personNode.firstChild.nodeValue;

var target = document.getElementById(“targetDiv”);

target.innerHTML=displayText;

}

function removeWhitespace(xml)

{

var loopIndex;

for (loopIndex = 0; loopIndex < xml.childNodes.length;loopIndex++)

{

var currentNode = xml.childNodes[loopIndex];

if (currentNode.nodeType == 1) {

removeWhitespace(currentNode);

}

if (((/^\s+$/.test(currentNode.nodeValue))) &&

(currentNode.nodeType == 3)) {

xml.removeChild(xml.childNodes[loopIndex--]);

}

}

}

</script>

</head>

<body>

<h1>Using Ajax and XML</h1>

<form>

<input type = “button” value = “Get the tech guy”

onclick = “getGuest()”>

</form>

<div id=”targetDiv” width =100 height=100>

Who is  the Techi???????????

</div>

</body>

</html>

 Students.xml

<?xml version=”1.0″?>

<students>

      <computerscience>

            <section-A>

                  <student1>Naveen</student1>

                  <student2>Niraj</student>

            </section-A>

            <section-B>

                  <student1>Radhesh</student1>

                  <student2>Rakesh</student>

            </section-B>

      </computerscience>

        <electronics>

            <section-A>

                  <student1>Balraj</student1>

                  <student2>Parthu</student>

            </section-A>

      </electronics>

</students>

Written by Naveen P.N

September 17, 2008 at 11:51 am

Leave a Reply