Monday 14 December 2015

Why does this work and can I do it better? XML DOMParser question.

Hello node.js developers and users! I am newer to javascript, but I'm working a in a job where everyone pushes me to use node.js but few can take the time to explain the basics.I work on a very monolithic tool for M&E and it spits out xml backup files and (in my use case) with way too much information such as object ownerships. So I'm attempting to remove them with the following:var xmlDP = new DOMParser().parseFromString(xmlFile,'text/xml'); //Get all user tags var x = xmlDP.getElementsByTagName("user"); var i = 0; var j = x.length; //remove all user tags for (i = 0; i < j; i++){ //console.log(i); x[i].parentNode.removeChild(x[i]); } If I do this:console.log(serializer.serializeToString(xmlDP)); Low and behold, it works!, but I don't know why.Why does removing a child from x affect the var xmlDP at all?Is there a better way to do this that is a little more self documenting?I had to put counters in a separate var (j) because it would quit early if I did i < x.length, why is that?If it is because x.length is a moving target in this case, why doesn't that moving target also get applied to j? What makes j static now?

Submitted December 14, 2015 at 05:03PM by travelphototim

No comments:

Post a Comment