Problem with looping over attributes in XML->DOM

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

    Problem with looping over attributes in XML->DOM

    Hi all,


    I'm coming across a problem, and really do not get where it comes
    from.

    The goal: to loop over attributes read from "object" nodes in an
    imported XML file/flow (via XMLHTTP) and transform them into HTML/DOM
    attributes.


    The function ObjectList reads from the imported XML and calls the
    needed functions to 'render' DOM nodes.

    function objectList(obje ct,usersrc) {
    var attlist=new Array();
    var objs = usersrc.getElem entsByTagName(o bject);
    for (i=0;i<objs.len gth;i++) {
    alert(objs.leng th); // reads 5, but the loop stops at 0 :(
    var obj;
    for (j=0;j<objs[i].attributes.len gth;j++) {
    attlist[j]='"'+objs[i].attributes[j].nodeName+'|'+o bjs[i].attributes[j].nodeValue+'"';
    }
    var objType = objs[i].getAttribute(' type');
    if (objType=='anch or') obj = new anchor(attlist) ;
    else {obj=null,alert ('no type specified');}
    // append child nodes
    document.getEle mentById(object ListId).appendC hild(obj);
    }
    }

    The anchor function creates the node in the DOM

    var a_att="..."; // list of valid W3C anchor attributes

    function anchor(attlist) {
    var att,attval;
    newanchor = document.create Element('a');
    for (i=0;i<attlist. length;i++) {
    att = attlist[i].substring(1,(a ttlist[i].indexOf(sep))) ;
    attval = attlist[i].substring(attl ist[i].indexOf(sep)+1 ,attlist[i].length-1);
    new Function ( 'if (a_att.indexOf( att)!=-1) {newanchor.' + att +
    '="' + attval + '";}' );
    }
    var anchortext = new textNode('test' );
    newanchor.appen dChild(anchorte xt);
    return newanchor;
    }

    If my imported XML file looks like this:

    <root>
    <menu type="anchor" href="test.jsp" cssClass="top"
    icon="test.gif" >overview</menu>
    <menu type="anchor" href="test.jsp" cssClass="top"
    icon="test.gif" >overview</menu>
    <menu type="anchor" href="test.jsp" cssClass="top"
    icon="test.gif" >overview</menu>
    <menu type="anchor" href="test.jsp" cssClass="top"
    icon="test.gif" >overview</menu>
    <menu type="anchor" href="test.jsp" cssClass="top"
    icon="test.gif" >overview</menu>
    </root>

    The ObjetList loop stops at i=0. Why? Any clue?

    Thanks :)
  • Dietmar Meier

    #2
    Re: Problem with looping over attributes in XML-&gt;DOM

    louissan wrote:
    [color=blue]
    > function objectList(obje ct,usersrc) {
    > [...]
    > for (i=0;i<objs.len gth;i++) {
    > [...]
    > if (objType=='anch or') obj = new anchor(attlist) ;
    > [...]
    > function anchor(attlist) {
    > [...]
    > for (i=0;i<attlist. length;i++) {
    > [...][/color]

    You use the same _global_ variable i in both functions.
    See ECMA-262 section 12.2 for details.

    ciao, dhgm

    Comment

    • Martin Honnen

      #3
      Re: Problem with looping over attributes in XML-&gt;DOM



      louissan wrote:

      [color=blue]
      > function objectList(obje ct,usersrc) {
      > var attlist=new Array();
      > var objs = usersrc.getElem entsByTagName(o bject);
      > for (i=0;i<objs.len gth;i++) {
      > alert(objs.leng th); // reads 5, but the loop stops at 0 :([/color]

      You need to learn to use local variables as much as possible, in
      particular loop variables should always be local to a function otherwise
      if you call into another function that use the same variable you get all
      sort of unwanted side effects.
      Thus use
      for (var i = 0; ...)[color=blue]
      > var obj;
      > for (j=0;j<objs[i].attributes.len gth;j++) {[/color]

      and
      for (var j = 0; ...)
      [color=blue]
      > function anchor(attlist) {
      > var att,attval;
      > newanchor = document.create Element('a');
      > for (i=0;i<attlist. length;i++) {[/color]

      and
      for (var i = 0;
      and then start again, your code is more likely to do what you want now.

      Note that I haven't checked all your code carefully, there might be
      other problems, but once you fix the variables to be local to the
      functions you can post back if there are still problems.


      --

      Martin Honnen

      Comment

      • louissan

        #4
        Re: Problem with looping over attributes in XML-&gt;DOM

        The local variables were indeed the problem :)

        Comment

        • Martin Honnen

          #5
          Re: Problem with looping over attributes in XML-&gt;DOM



          louissan wrote:
          [color=blue]
          > The local variables were indeed the problem :)[/color]

          No, the lack of local variables were the problem.


          --

          Martin Honnen

          Comment

          Working...