472,961 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,961 software developers and data experts.

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(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 :)
Jul 23 '05 #1
4 1938
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
Jul 23 '05 #2


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/
Jul 23 '05 #3
The local variables were indeed the problem :)
Jul 23 '05 #4


louissan wrote:
The local variables were indeed the problem :)


No, the lack of local variables were the problem.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
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...
4
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...
4
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...
4
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...
5
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...
1
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()...
2
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...
6
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...
9
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...
10
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',...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.