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(object,usersrc) {
var attlist=new Array();
var objs = usersrc.getElementsByTagName(object);
for (i=0;i<objs.length;i++) {
alert(objs.length); // reads 5, but the loop stops at 0 :(
var obj;
for (j=0;j<objs[i].attributes.length;j++) {
attlist[j]='"'+objs[i].attributes[j].nodeName+'|'+objs[i].attributes[j].nodeValue+'"';
}
var objType = objs[i].getAttribute('type');
if (objType=='anchor') obj = new anchor(attlist);
else {obj=null,alert('no type specified');}
// append child nodes
document.getElementById(objectListId).appendChild( 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.createElement('a');
for (i=0;i<attlist.length;i++) {
att = attlist[i].substring(1,(attlist[i].indexOf(sep)));
attval = attlist[i].substring(attlist[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.appendChild(anchortext);
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 :) 4 1955
louissan wrote: function objectList(object,usersrc) { [...] for (i=0;i<objs.length;i++) { [...] if (objType=='anchor') obj = new anchor(attlist); [...] function anchor(attlist) { [...] for (i=0;i<attlist.length;i++) { [...]
You use the same _global_ variable i in both functions.
See ECMA-262 section 12.2 for details.
ciao, dhgm
louissan wrote: function objectList(object,usersrc) { var attlist=new Array(); var objs = usersrc.getElementsByTagName(object); for (i=0;i<objs.length;i++) { alert(objs.length); // reads 5, but the loop stops at 0 :(
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; ...) var obj; for (j=0;j<objs[i].attributes.length;j++) {
and
for (var j = 0; ...)
function anchor(attlist) { var att,attval; newanchor = document.createElement('a'); for (i=0;i<attlist.length;i++) {
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 http://JavaScript.FAQTs.com/
The local variables were indeed the problem :)
louissan wrote: The local variables were indeed the problem :)
No, the lack of local variables were the problem.
--
Martin Honnen http://JavaScript.FAQTs.com/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Hans Nowak |
last post by:
Howdy y'all,
The following works in Python 2.2.2:
Python 2.2.2 (#37, Oct 14 2002, 17:02:34) on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from...
|
by: Carlo Sette |
last post by:
Hi all,
when I try to Parse a sample XML ducument Python display the follow error
message:
---------------------------------------------------------------------------
Traceback (most recent call...
|
by: Peter Maas |
last post by:
Hi,
I have a problem parsing html text with xmldom. The following code
runs well:
--------------------------------------------
from xml.dom.ext.reader import HtmlLib
from xml.dom.ext import...
|
by: Skip Montanaro |
last post by:
I'm getting somewhat painfully acquainted with xml.dom.minidom. What is the
relationship between its documentElement attribute and its childNodes list?
I thought XML documents consisted of a...
|
by: Mike McGavin |
last post by:
Hi everyone.
I've been trying for several hours now to get minidom to parse
namespaces properly from my stream of XML, so that I can use DOM methods
such as getElementsByTagNameNS(). For some...
|
by: Greg Wogan-Browne |
last post by:
Hi all,
I am having some trouble figuring out what is going on here - is this a
bug, or correct behaviour? Basically, when I create an XML document with
a namespace using xml.dom.minidom.parse()...
|
by: user |
last post by:
Hi
How do i remove comments from a DOM object ?
I tried searching the man pages XML::DOM, XML::DOM::Comment and
XML::DOM::Document but could not find anything
The code used to parse the...
|
by: Dan |
last post by:
I'm using python's xml.dom.minidom module to generate xml files, and
I'm running into memory problems. The xml files I'm trying to create
are relatively flat, with one root node which may have...
|
by: Lie |
last post by:
Why this generates AttributeError, then not?
Python 2.5.2 (r252:60911, Apr 21 2008, 11:17:30)
on linux2
Type "help", "copyright", "credits" or "license" for more information.
Traceback (most...
|
by: Simon Willison |
last post by:
I'm having a horrible time trying to get xml.dom.pulldom to consume a
UTF8 encoded XML file. Here's what I've tried so far:
<msg>Simon\xe2\x80\x99s XML nightmare</msg>
"""
('START_DOCUMENT',...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
| |