JavaScript XML Parsing Works in IE but not in Firefox/Chrome

reidasyor

New Member
I have the following problem with XML parsing for which my script works in IE but not in Firefox/Chrome (which is weird). Sure it's something really stupid I'm doing but any help much appreciated.The following JavaScript function:\[code\]this.displayData = http://stackoverflow.com/questions/11002288/function(xml){ var table = document.createElement("table"); // table for results table.border=0; table.width="100%"; table.classname="requestList"; var container = document.getElementById('mainright'); if (container.hasChildNodes()) { while(container.childNodes.length >= 1) container.removeChild(container.firstChild); } container.appendChild(table); var entities = xml.getElementsByTagName("entity"); for (var i=0; i<entities.length; ++i) { var entity = entities; var row = table.insertRow(table.getElementsByTagName("tr").length); var fields = entity.getElementsByTagName("field"); for (var z=0; z<fields.length; ++z) { var cell = row.insertCell(z); cell.innerHTML = "("+fields[z].childNodes[0].nodeValue+")"; } }}\[/code\]Is being called as an xmlhttp callback and the XML seems valid. Sample of the XML output:\[code\]<data-list><entity entity=""><field field="requestid"><![CDATA[ 1 ]]></field><field field="customer"><![CDATA[ 1 ]]></field><field field="assignteam"><![CDATA[ 0 ]]></field><field field="assignuser"><![CDATA[ admin ]]></field><field field="class"><![CDATA[ 1 ]]></field><field field="openeddt"><![CDATA[ 2012-06-11 19:39:26 ]]></field><field field="status"><![CDATA[ 1 ]]></field></entity><entity entity=""><field field="requestid"><![CDATA[ 2 ]]></field><field field="customer"><![CDATA[ 1 ]]></field><field field="assignteam"><![CDATA[ 0 ]]></field><field field="assignuser"><![CDATA[ admin ]]></field><field field="class"><![CDATA[ 1 ]]></field><field field="openeddt"><![CDATA[ 2012-06-11 19:40:02 ]]></field><field field="status"><![CDATA[ 1 ]]></field></entity></data-list>\[/code\]In IE when I make the request it displays the data in a newly created table, one row per entity and one column per field.However in Firefox and Chrome the request is called fine and generates no errors but empty strings are returned as the nodeValues, e.g. all I get is () in each of the (correct number of) rows and columns.I'm sure I am just being really stupid. Any help much appreciated.Cheers,Dave.** ANSWER I HOPE BUT I CAN'T ANSWER MY OWN QUESTION FOR ANOTHER 7 HOURS **Right so a minute after asking I seem to have stumbled on the answer...textContentThis seems to be the attribute used in Firefox and Chrome for text data (I'm assuming because it was enclosed in CDATA tags). However changing the code to fields[z].textContent then breaks IE.So I've done the following:\[code\]var data = http://stackoverflow.com/questions/11002288/(fields[z].textContent == undefined) ? fields[z].firstChild.nodeValue : fields[z].textContent;cell.innerHTML = data;\[/code\]Which seems to work ok in both.But I would welcome any "proper" solutions.Cheers,Dave.
 
Back
Top