View Full Version : parsing xml in JS - deleting whitespace in mozilla

06-24-2006, 05:47 AM
I'm writing a JavaScript that loads an XML file, parses the info, and pushes it into an array. The script needs to work in IE and and Firefox.Loading the XML file is easy enough.Parsing the file in IE is pretty simple as well as is pushing the info into an array.

The problem occurs when in the function I'm using to remove the whitespace from the xml file for firefox or netscape.

Here is the function I'm using. It's called parseXml()

1.) function parseXml() {
2.) var msgObj = xmlDoc.getElementsByTagName("messages")[0];
3.) for (i=0;i<msgObj.childNodes.length - 1;i++) {
4.) if ((msgObj.childNodes[i].nodeType == 3) && (!notWhitespace.test(msgObj.childNodes[i].nodeValue))) {
5.) // i--;
6.) }
7.) photoArray.push(msgObj.childNodes[i].firstChild.nodeValue);
8.) }
9.) }

Here is the content of the xml file I'm testing with - daily.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<daily>Today is Sunday.</daily>
<daily>Today is Monday.</daily>
<daily>Today is Tuesday.</daily>
<daily>Today is Wednesday.</daily>
<daily>Today is Thursday.</daily>
<daily>Today is Friday.</daily>
<daily>Today is Saturday.</daily>

The script loads the xml file just fine in both IE and Firefox.

The problem occurs w/in parseXml(); As it is it works great in Firefox. But with IE it leaves out the info - Today is Saturday - the last daily tag.

It does this because of the "-1" in line 3:
for (i=0;i<msgObj.childNodes.length - 1;i++)

Apparently the line should read:
for (i=0;i<msgObj.childNodes.length;i++)
and line 5 should be used rather than commented out. But if i do this - firefox generates an error from line 7 - I believe it has something to do with the last value of i in the for loop being a whitespace.

Any ideas on this. I've the html doc and the xml doc as txt files in case that helps out.

06-24-2006, 09:52 AM
why not just say
xmlDoc.getElementsByTagName("messages")[0].getElementsByTagName("daily")or if you have access to it before its node-ified, this regexp will kill all whitespace


06-24-2006, 09:56 AM
when this occurs


then the number of child nodes decreases and so does the count and the number of child nodes

tis necessary to first load the child nodes into an array and loop this array to remove the child nodes from their parent

06-26-2006, 09:03 AM
That worked perfectly. Thanks so much.