getAttribute problem when using XML with JavaScript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mr_burns

    getAttribute problem when using XML with JavaScript

    Hi,

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


    var xmlDoc = new ActiveXObject(" Microsoft.XMLDO M");
    xmlDoc.async="f alse";
    xmlDoc.load("no te.xml");
    nodes = xmlDoc.document Element.childNo des;
    text = '<p>note:<br>' ;
    for (var i=0; i<nodes.length ; i++) {
    title = nodes.item(i).f irstChild.text;
    value = nodes.item(i).f irstChild.getAt tribute('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>
  • Martin Honnen

    #2
    Re: getAttribute problem when using XML with JavaScript



    mr_burns wrote:

    [color=blue]
    > I have the following code that works fine except the getAttribute line:
    >
    >
    > var xmlDoc = new ActiveXObject(" Microsoft.XMLDO M");
    > xmlDoc.async="f alse";
    > xmlDoc.load("no te.xml");
    > nodes = xmlDoc.document Element.childNo des;
    > text = '<p>note:<br>' ;
    > for (var i=0; i<nodes.length ; i++) {
    > title = nodes.item(i).f irstChild.text;
    > value = nodes.item(i).f irstChild.getAt tribute('id');[/color]

    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).t ext;
    value = nodes.item(i).g etAttribute('id ');

    --

    Martin Honnen

    Comment

    Working...