I'm loading in an XML file in JavaScript, and once I've done that, I'm iterating through all of the Document's childNodes
, which are all of the XML elements in the document. The problem that I'm having is that I need to ignore elements which are just not real elements, but rather newlines, tabs, etc. Right now I'm doing the following:
for (var i = 0; i < childList.length; i++)
{
switch (childList[i].nodeType)
{
case 1: // Node.ELEMENT_NODE
/* snip */
break;
case 3: // Node.TEXT_NODE
case 8: // Node.COMMENT_NODE
// Ensure the node is a valid node, and not newlines, tabs, etc
if (!nodeEmpty(childList[i].nodeValue)
{
// do stuff
}
break;
}
}
function nodeEmpty(nodeValue)
{
var isEmpty = true;
var length = nodeValue.length;
for (var i = 0; i < length; i++)
{
if (nodeValue[i] != ' ' && nodeValue[i] != '\n' && nodeValue[i] != '\t')
{
isEmpty = false;
break;
}
}
return isEmpty;
}
But this seems like a very non-elegant way of achieving this. Is there a better way of doing this?