Friday, November 17, 2006

Mozilla XML DOM and nodeValue length - Revisited

I have switched over from my old blogging engine to I only had one entry, but I wanted to go ahead and post it. Here is the text of the old post:

I discovered a behavior in the Mozilla XML DOM which is different than I would have suspected, and not well documented. At least I was not able to find out this info without much frustration. Suppose that you to extract information out of a parsed XML file via the xmlHttpResponse object as the code fragment below demonstrates:

reqObj = new XMLHttpRequest();
if (reqObj.readyState == 4) {
// only if "OK"
if (reqObj.status == 200) {

//Get XML data from response
var data = reqObj.responseXML.getElementsByTagNameNS "http://namespace/", "getDataResult");

var text = data[0].childNodes[0].nodeValue;

I expected the last line above to return the entire string value of the element's text for the node. Instead what I received was the a trunctated version of the text value. The equivalent operation using the IE DOM object worked just fine, and did not truncate the text.

var text = data[0].childNodes[0].text; //for IE worked fine

It turns out that in the Mozilla XML DOM if the text value of the node is too long it will create child nodes to hold the rest of the text.

The following modification took care of the problem and I was able to retrieve the entire text value for the node:

var text = "";
for(var cnt=0; cnt <>
text = text + data[0].childNodes[cnt].nodeValue;

Another reminder to not get sloppy and just assume the length of a collection that is returned.

No comments: