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. Advertisements

  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. Advertisements

  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. Advertisements

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. Dj Frenzy

    JAXB to get XML attributes

    Dj Frenzy, Dec 16, 2004, in forum: Java
    Replies:
    0
    Views:
    718
    Dj Frenzy
    Dec 16, 2004
  2. Jordan Willms
    Replies:
    2
    Views:
    956
    Richard Tobin
    Nov 9, 2004
  3. kellner
    Replies:
    0
    Views:
    983
    kellner
    Jul 23, 2006
  4. Max
    Replies:
    1
    Views:
    642
    Joe Kesselman
    Sep 22, 2006
  5. Replies:
    3
    Views:
    4,424
  6. Web Developer

    Use XML attributes to specify another XML name or namespace...

    Web Developer, Feb 17, 2005, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    1,103
    Web Developer
    Feb 17, 2005
  7. Erik Wasser
    Replies:
    5
    Views:
    816
    Peter J. Holzer
    Mar 5, 2006
  8. NorbertH
    Replies:
    1
    Views:
    149
Loading...