getAttribute problem when using XML with JavaScript

Discussion in 'Javascript' started by mr_burns, Dec 24, 2004.

  1. mr_burns

    mr_burns Guest

    Hi,

    I have the following code that works fine except the getAttribute line:


    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async="false";
    xmlDoc.load("note.xml");
    nodes = xmlDoc.documentElement.childNodes;
    text = '<p>note:<br>';
    for (var i=0; i<nodes.length; i++) {
    title = nodes.item(i).firstChild.text;
    value = nodes.item(i).firstChild.getAttribute('id');
    text+= title+':'+value;
    }
    text+= '</p>';


    an example of the xml nodes are:


    <?xml...
    <all>
    <node id="1">text</node>
    <node id="2">text</node>
    <node id="3">text</node>
     
    mr_burns, Dec 24, 2004
    #1
    1. Advertising

  2. mr_burns wrote:


    > I have the following code that works fine except the getAttribute line:
    >
    >
    > var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    > xmlDoc.async="false";
    > xmlDoc.load("note.xml");
    > nodes = xmlDoc.documentElement.childNodes;
    > text = '<p>note:<br>';
    > for (var i=0; i<nodes.length; i++) {
    > title = nodes.item(i).firstChild.text;
    > value = nodes.item(i).firstChild.getAttribute('id');


    Well what exactly goes wrong? Do you get an error? Or is the result not
    what you expect?
    In the DOM object model not every child node needs to be an element
    node, and only element nodes implement the getAttribute.
    What you are trying to do is access a child node of an element node and
    in your example those child nodes are text nodes and thus do not have a
    method getAttribute.
    With MSMXL you will probably get the result you are looking for if you
    change your code to use the lines
    title = nodes.item(i).text;
    value = nodes.item(i).getAttribute('id');

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 24, 2004
    #2
    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. DN
    Replies:
    2
    Views:
    11,743
  2. Replies:
    3
    Views:
    2,862
  3. Rob
    Replies:
    4
    Views:
    8,548
    Brian A Palmer
    Sep 14, 2003
  4. ICPooreMan
    Replies:
    4
    Views:
    123
    Wenzel.Peppmeyer
    Apr 7, 2007
  5. Walton
    Replies:
    0
    Views:
    609
    Walton
    Feb 15, 2008
Loading...

Share This Page