RangeError: Maximum call stack size exceeded

81 views
Skip to first unread message

Udayantha Warnasuriya

unread,
Dec 16, 2012, 9:45:50 AM12/16/12
to libx...@googlegroups.com
Hi I'm writing a function to replace all text tags with integer values in a xml file. which will be '<child>' will be replaced as '<1>'. Here is my code so far;

var libxml = require('libxmljs');
var xml =  '<?xml version="1.0" encoding="UTF-8"?>' +
           '<root>' +
               '<child foo="bar">' +
                   '<grandchild baz="fizbuzz">grandchild content</grandchild>' +
               '</child>' +
               '<child foo="bar1">' +
                   '<grandchild baz="fizbuzz">grandchild content 1</grandchild>' +
               '</child>' +
               '<sibling>with content!</sibling>' +
           '</root>';


var xmlDoc = libxml.parseXml(xml);
var allxml = xmlDoc.root();
var allNodes = xmlDoc.childNodes();
//console.log(allNodes.length);
rec(allxml);

function rec(anElement){
for (var j=0; j<allNodes.length;j++ )
    {
        var firstnode = allNodes[j].name();
   
        var findelem = xmlDoc.find(firstnode);
        var currChild = xmlDoc.child(j);
        var currnode = xmlDoc.childNodes();
        if (hasChild(currChild)) {
                rec(currChild.childNodes());
            }
        else{ replaceCurrentTag(findelem, j);}
    }
}

function replaceCurrentTag(currelem, j){

for (var i=0;i<currelem.length;i++){
        currelem[i].name(j.toString());
    }
}

function hasChild(xmlElement){
var e = xmlElement.childNodes();
if (e.length > 0){ return true;    }
else return false;
}

console.log(xmlDoc.toString());


But I'm getting this error when I run the code. What am I doing wrong in here. Please help;

/home/compaq/node_modules/libxmljs/lib/document.js:0
(function (exports, require, module, __filename, __dirname) { var bindings = r
^
RangeError: Maximum call stack size exceeded


marco....@gmail.com

unread,
Dec 16, 2012, 3:15:26 PM12/16/12
to libx...@googlegroups.com
This is a problem with your code. You are always looping over allNodes in your recursive function. Whenever you see a call stack error and you're using recursion, that should make you suspect something. Check your code carefully, do some logging to the console. Learning to debug is the first step in getting something working. Good luck.

:Marco

Sent from my iPhone
Reply all
Reply to author
Forward
0 new messages