XMLHttpRequest - how to get XML attributes

Discussion in 'ASP .Net' started by SpaceMarine, Jun 20, 2008.

  1. SpaceMarine

    SpaceMarine Guest

    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
     
    SpaceMarine, Jun 20, 2008
    #1
    1. Advertising

  2. SpaceMarine

    SpaceMarine Guest

    On Jun 20, 11:38 am, SpaceMarine <> 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/library/ms757878(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.
     
    SpaceMarine, Jun 20, 2008
    #2
    1. Advertising

  3. SpaceMarine

    SpaceMarine Guest

    On Jun 20, 11:53 am, SpaceMarine <> 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.attributes.getNamedItem("value").value);



    sm
     
    SpaceMarine, Jun 20, 2008
    #3
  4. SpaceMarine

    Joe Fawcett Guest

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

    You should also be able to use the terser:

    resultNodes.getAttribute("value")

    --

    Joe Fawcett (MVP - XML)
    http://joe.fawcett.name
     
    Joe Fawcett, Jun 20, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Max
    Replies:
    1
    Views:
    484
    Joe Kesselman
    Sep 22, 2006
  2. P4trykx
    Replies:
    2
    Views:
    1,827
    bruce barker
    Jan 31, 2007
  3. Replies:
    3
    Views:
    2,875
  4. james_027

    class attributes & data attributes

    james_027, Jun 20, 2007, in forum: Python
    Replies:
    2
    Views:
    377
    Bruno Desthuilliers
    Jun 20, 2007
  5. NorbertH
    Replies:
    1
    Views:
    82
Loading...

Share This Page