Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > XMLHttpRequest - how to get XML attributes

Reply
Thread Tools

XMLHttpRequest - how to get XML attributes

 
 
SpaceMarine
Guest
Posts: n/a
 
      06-20-2008
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[i].value = i; // ? how do i get the attribute of the
node?
ddlItems.options[i].text = resultNodes[i].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
 
Reply With Quote
 
 
 
 
SpaceMarine
Guest
Posts: n/a
 
      06-20-2008
On Jun 20, 11:38*am, SpaceMarine <(E-Mail Removed)> wrote:

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


found something helpful:
http://msdn.microsoft.com/en-us/libr...78(VS.85).aspx

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.

 
Reply With Quote
 
 
 
 
SpaceMarine
Guest
Posts: n/a
 
      06-20-2008
On Jun 20, 11:53*am, SpaceMarine <(E-Mail Removed)> wrote:

> 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[i].attributes.getNamedItem("value").value);



sm
 
Reply With Quote
 
Joe Fawcett
Guest
Posts: n/a
 
      06-20-2008


"SpaceMarine" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Jun 20, 11:53 am, SpaceMarine <(E-Mail Removed)> wrote:
>
>> 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[i].attributes.getNamedItem("value").value);
>
>
>
> sm

You should also be able to use the terser:

resultNodes[i].getAttribute("value")

--

Joe Fawcett (MVP - XML)
http://joe.fawcett.name

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
web.xml / XML schema issue, why do some XML schema attributes disappear asciz@starmail.com Java 3 02-20-2007 09:56 AM
WebControl.Attributes.Add and custom attributes P4trykx ASP .Net 2 01-31-2007 04:33 PM
How to split one XML into smaller XML; received via a XMLHttpRequest? NorbertH Javascript 1 01-04-2007 01:30 AM
Parse reserved attributes as normal attributes Max XML 1 09-22-2006 12:04 PM



Advertisments