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

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:

this.displayData = 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[i];
        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+")";
        }
    }
}

Is being called as an xmlhttp callback and the XML seems valid. Sample of the XML output:

<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>

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...

textContent

This 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:

var data = (fields[z].textContent == undefined) ? fields[z].firstChild.nodeValue : fields[z].textContent;
cell.innerHTML = data;

Which seems to work ok in both.

But I would welcome any "proper" solutions.

Cheers,

Dave.

Answers


This transpired to be a header problem - stupidly I wasn't setting the output header in the XML-generation to text/xml. Although IE didn't care FF and Chrome did.

Setting the Content-type header in PHP solved it.


Need Your Help

mysql weird trigger errors

mysql triggers

I'm trying to set up a trigger (run from a separate .sql file) to prevent insertion like this:

Caching Pattern for read-through/write-behind cache with fallback to underlying data store

design-patterns caching appfabric

I'm looking to implement a caching layer with read-through/write-behind approach, but possibility to fall back to directly accessing data store in case of cache failure.