XMLHttpRequest - how to get XML attributes

  • Thread starter Thread starter SpaceMarine
  • Start date Start date
S

SpaceMarine

hello,

my ASP.NET page uses javascript to retrieve XML data from another ASPX
page. i do this like so (snippets for example):


var ajaxResults;
var xmlhttp = new XMLHttpRequest();

xmlhttp.open("GET", "getData.aspx", true);

xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4) //ready
{
if (xmlhttp.status == 200) //success
{
//alert('success');
var xml = xmlhttp.responseXML;

//return cross-browser DOM object
if (xml.documentElement)
ajaxResults = xml.documentElement;

} else {
//alert('There was a problem retrieving the data:\n' +
xmlhttp.statusText);
}
}
}

xmlhttp.send(null)


....which populates "ajaxResults". the data looks like:


<results timestamp="6/20/2008 11:34:33 AM">
<dataItem value="1">Call for Service</dataItem>
</results>


....cool. i can then use this in JS like so:


var ddlItems = document.getElementById("ddlItems");

var i;
for (i = 0; i < resultNodes.length; i++){

//add an item to list
ddlItems.options.length ++;
ddlItems.options.value = i; // ? how do i get the attribute of the
node?
ddlItems.options.text = resultNodes.firstChild.nodeValue; //
gets TEXT of "<dataItem>"
}


....all well and good (enough for me). but the question: how do i parse
the *attribute* values from my above returned XML object?


thanks,
matt
 
so in the above, heres how i can get the root node's timestamp
attribute:

   alert(ajaxResults.attributes[0].value);

...now all i have to do is get the <dataItem>'s attribute. will post
syntax when located.

found it:

//attribute value for result node
alert(resultNodes.attributes.getNamedItem("value").value);



sm
 
SpaceMarine said:
so in the above, heres how i can get the root node's timestamp
attribute:

alert(ajaxResults.attributes[0].value);

...now all i have to do is get the <dataItem>'s attribute. will post
syntax when located.

found it:

//attribute value for result node
alert(resultNodes.attributes.getNamedItem("value").value);



sm

You should also be able to use the terser:

resultNodes.getAttribute("value")
 
Back
Top