6

The following code works in Chrome, but not IE, or FireFox. Does someone know the appropriate cross browser code?

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
    <a:Action s:mustUnderstand="1">http://tempuri.org/SubscriptionService/Update</a:Action>
    <netdx:Duplex xmlns:netdx="http://schemas.microsoft.com/2008/04/netduplex">
        <netdx:Address>http://docs.oasis-open.org/ws-rx/wsmc/200702/anonymous?id=4ed8a7ee-b124-e03e-abf0-a294e99cff73</netdx:Address>
        <netdx:SessionId>177b4f47-5664-d96c-7ffa-0a8d879b67dd</netdx:SessionId>
    </netdx:Duplex>
</s:Header>
<s:Body>
    <Update xmlns="http://tempuri.org/">
        <lstResponseStruct xmlns:b="http://schemas.datacontract.org/2004/07/FSS.Libs.Core.InterprocessData.RMS" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <b:DataA>
                <b:ValueA>1.339565</b:ValueA>
                <b:Status>None</b:Status>
            </b:DataA>
            <b:DataA>
                <b:ValueA>120.3717</b:ValueA>
                <b:Status>None</b:Status>
            </b:DataA>
            <b:DataA>
                <b:ValueA>133.563116</b:ValueA>
                <b:Status>None</b:Status>
            </b:DataA>
            <b:DataA>
                <b:ValueA>-0.0059159999999999994</b:ValueA>
                <b:Status>None</b:Status>
            </b:DataA>
        </lstResponseStruct>
    </Update>
</s:Body>

Here are the JavaScript snippets...

<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>

var nodes;
if (typeof DOMParser != "undefined")
    nodes = ((new DOMParser()).parseFromString(request.responseText, "application/xml")).getElementsByTagName("*");
else {
    request.responseXML.loadXML(request.responseText);
    nodes = request.responseXML.getElementsByTagName("*");
} 

for (var i = 0; i < nodes.length; i++) {
    var element = nodes[i];
    ...
    if ((element.localName == "Body" || element.baseName == "Body") && element.namespaceURI == "http://www.w3.org/2003/05/soap-envelope") {
        body = element;
        break;
}

$(body).find('DataA').each(function () {
    ... Do something
}

for some reason, in each browser "body" definitely contains the body xml, however the $(body).find('DataA') doesn't return results for IE or FireFox.

Update:

Adding the namespace $(body).find('b\\:DataA') works well for FireFox and IE, but breaks Chrome!

3
  • 4
    Tip: Use jQuery's $.parseXML method. It returns an XMLDocument object (not a jQuery object). To get a jQuery object, wrap the return value: $($.parseXML('<x>Test</x>')); Commented Apr 16, 2012 at 21:14
  • Better yet, since this appears to be an ajax request, use jquery's ajax and set the dataType appropriately (or let jquery figure it out) and you won't even need to call parseXML directly. Commented Apr 16, 2012 at 21:24
  • $(xData.responseXML).find("z\\:row, row").each(function() { // Do Stuff }); Seems to work for IE, FireFox, and Chrome. Commented Apr 16, 2012 at 21:48

2 Answers 2

12

It was a problem accessing XML nodes without the namespace specified. For some reason Chrome doesn't want to see the namespace.

I found that the "b\:DataA" selector works for FireFox and IE, and the "DataA" selector works for Chrome.

so...

$(xData.responseXML).find("b\\:DataA, DataA").each(function() { // Do Stuff }); 

Seems to work for IE, FireFox, and Chrome.

see http://www.steveworkman.com/html5-2/javascript/2011/improving-javascript-xml-node-finding-performance-by-2000/ for more information and ways to improve XML node finding performance.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, we have the same problem. +1 for solving my problem.
Thanks, this solved my problem. Oh, and it works in Safari too for that matter.. :-)
0

It's working fine!!! Try this,

Chrome/Firefox:

xml.children[0].childNodes[1].innerHTML;

IE8+/Safari:

xml.childNodes[0].childNodes[1].textContent;

IE8:

xml.documentElement.childNodes[1].text;

Sample code here,

var xml = $.parseXML(XMLDOC); 

Var xmlNodeValue = ""; 

if(userAgent.match("firbox")){

xml.children[0].childNodes[1].innerHTML;

}else{ // IE8+

xmlNodeValue = xml.childNodes[0].childNodes[1].textContent; 

}

1 Comment

referencing using hard coded position index is not flexible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.