First page Back Continue Last page Image

Reading Server Data

<p>

<a id="makeTextRequest" href="gAddress.txt">Request a text file</a><br>

<a id="makeXMLRequest" href="us-states.xml">Request an XML file</a>

</p>

<div id="updateArea"> </div>

<?xml version="1.0"?>

<choices xml:lang="EN">

<item><label>Alabama</label><value>AL</value></item>

<item><label>Alaska</label><value>AK</value></item>

<item><label>Arizona</label><value>AZ</value></item>

<item><label>Arkansas</label><value>AR</value></item>

<item><label>California</label><value>CA</value></item>

<item><label>Colorado</label><value>CO</value></item>

function showContents() {

if (xhr.readyState == 4) {

if (xhr.status == 200) {

if (xhr.responseXML && xhr.responseXML.childNodes.length > 0) {

var outMsg = getText(xhr.responseXML.getElementsByTagName("choices")[0]);

}

else {

var outMsg = xhr.responseText;

}

}

else {

var outMsg = "There was a problem with the request " + xhr.status;

}

document.getElementById("updateArea").innerHTML = outMsg;

}

function getText(inVal) {

if (inVal.textContent) {

return inVal.textContent;

}

return inVal.text;

}

}

3 Parts:

1) Create xhr (XmlHttpRequest)

2) Evaluate xhr

3) Output xhr's data (outMsg)

readyState Values

0

Uninitialized

1

Open & Loading

2

Headers Loaded

3

Loading & Useable

4

Complete

us-states.xml

I only get here if I have a valid xhr Object

The childNodes (a property) are the subordinate nodes to the selected node