node-xmlreader icon indicating copy to clipboard operation
node-xmlreader copied to clipboard

Empty tags result into error

Open rudymatos opened this issue 7 years ago • 1 comments

When having an empty tag like :

and trying to get text() from value tag library returns "TypeError: xml.value.text() is not a function."

rudymatos avatar Jul 16 '17 23:07 rudymatos

Hi, I know it's a very old post, but I had the same problem and I fixed it by adding few lines to the module. Basically what is happening is that only the nodes that have any text have the method text(). It's caused by method text() only being added to objects that invoke ontext event from sax module (which is a module that xmlreader is based on).

So here is my solution (xmlreader.js file):

 	saxparser.onclosetag = function (node) {
 		// add text method to objects without text
 		if(!object.text){
 			object.text = function(){
 				return null;
 			}
 		}

 		// set the object back to its parent:
 		object = object.parent();
 	}

I'm checking in onclosetag event if an object has text() method. If it doesn't then I just create one that returns null. So now when you try to get text of an empty tag, it's just going to return null instead of crashing.

I think this kind of fix should be added to the official module, @SamDecrock

I love the module though :)

jaruszewskiG avatar Dec 21 '17 23:12 jaruszewskiG