XMLHttpRequest - how to get XML attributes

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
 
S

SpaceMarine

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
 
J

Joe Fawcett

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")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top