IE 8 JQuery .find() not working on XML string

2019-04-16 09:34发布

问题:

JQuery does not seem to be able to find my selector string inside of a returned XML string. It appears to be working in Chrome, but I can't debug in the same environment due to browser authentication issues.

I know these type of questions have been asked in the past, but I haven't found anything that specifically applies to my situation.

The following XML response is supplied from a SOAP request; I believe the <z:row piece is what matters here:

"<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/"><GetListItemsResult><listitems xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
                <rs:data ItemCount="6">
                    <z:row ows_Attachments="0" ows_Year="2012" ows_LinkTitle="Orange" ows_Quantity="3.00000000000000" ows_Quantity2="5.00000000000000" ows_Color="Orange" ows__ModerationStatus="0" ows__Level="1" ows_Title="Orange" ows_ID="1" ows_UniqueId="1;#{FEDE3004-A0F4-421F-A76B-5BD51003B11C}" ows_owshiddenversion="8" ows_FSObjType="1;#0" ows_Created_x0020_Date="1;#2012-08-01 19:24:08" ows_Created="2012-08-01 19:24:08" ows_FileLeafRef="1;#1_.000" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-08-15 11:15:41" ows_FileRef="1;#Sandbox/bitest/Lists/Produce/1_.000" ows_MetaInfo="1;#"/>
                    <z:row ows_Attachments="0" ows_Year="2012" ows_LinkTitle="Apple" ows_Quantity="5.00000000000000" ows_Quantity2="9.00000000000000" ows_Color="Red" ows__ModerationStatus="0" ows__Level="1" ows_Title="Apple" ows_ID="2" ows_UniqueId="2;#{1E40887F-87BF-4029-93A3-A1096DF1D1A7}" ows_owshiddenversion="8" ows_FSObjType="2;#0" ows_Created_x0020_Date="2;#2012-08-01 19:24:42" ows_Created="2012-08-01 19:24:42" ows_FileLeafRef="2;#2_.000" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-08-15 11:15:42" ows_FileRef="2;#Sandbox/bitest/Lists/Produce/2_.000" ows_MetaInfo="2;#"/>
                    <z:row ows_Attachments="0" ows_Year="2011" ows_LinkTitle="Orange" ows_Quantity="4.00000000000000" ows_Quantity2="9.00000000000000" ows_Color="Orange" ows__ModerationStatus="0" ows__Level="1" ows_Title="Orange" ows_ID="4" ows_UniqueId="4;#{D3C73386-1EA5-435A-BD1A-EF67F719078F}" ows_owshiddenversion="7" ows_FSObjType="4;#0" ows_Created_x0020_Date="4;#2012-08-15 11:13:23" ows_Created="2012-08-15 11:13:23" ows_FileLeafRef="4;#4_.000" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-08-15 11:15:36" ows_FileRef="4;#Sandbox/bitest/Lists/Produce/4_.000" ows_MetaInfo="4;#"/>
                    <z:row ows_Attachments="0" ows_Year="2011" ows_LinkTitle="Apple" ows_Quantity="8.00000000000000" ows_Quantity2="12.0000000000000" ows_Color="Red" ows__ModerationStatus="0" ows__Level="1" ows_Title="Apple" ows_ID="5" ows_UniqueId="5;#{13F99513-8DE1-48A9-9114-40E5A8B2343F}" ows_owshiddenversion="6" ows_FSObjType="5;#0" ows_Created_x0020_Date="5;#2012-08-15 11:13:59" ows_Created="2012-08-15 11:13:59" ows_FileLeafRef="5;#5_.000" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-08-15 11:15:27" ows_FileRef="5;#Sandbox/bitest/Lists/Produce/5_.000" ows_MetaInfo="5;#"/>
                    <z:row ows_Attachments="0" ows_Year="2010" ows_LinkTitle="Orange" ows_Quantity="2.00000000000000" ows_Quantity2="6.00000000000000" ows_Color="Orange" ows__ModerationStatus="0" ows__Level="1" ows_Title="Orange" ows_ID="6" ows_UniqueId="6;#{A6DBFD00-6383-4993-9C9E-4398E080FDEE}" ows_owshiddenversion="6" ows_FSObjType="6;#0" ows_Created_x0020_Date="6;#2012-08-15 11:14:05" ows_Created="2012-08-15 11:14:05" ows_FileLeafRef="6;#6_.000" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-08-15 11:15:38" ows_FileRef="6;#Sandbox/bitest/Lists/Produce/6_.000" ows_MetaInfo="6;#"/>
                    <z:row ows_Attachments="0" ows_Year="2010" ows_LinkTitle="Apple" ows_Quantity="10.0000000000000" ows_Quantity2="2.00000000000000" ows_Color="Red" ows__ModerationStatus="0" ows__Level="1" ows_Title="Apple" ows_ID="7" ows_UniqueId="7;#{2F1BD539-545A-4796-AC1D-A9C4B76BE253}" ows_owshiddenversion="4" ows_FSObjType="7;#0" ows_Created_x0020_Date="7;#2012-08-15 11:14:58" ows_Created="2012-08-15 11:14:58" ows_FileLeafRef="7;#7_.000" ows_PermMask="0x7fffffffffffffff" ows_Modified="2012-08-15 11:15:28" ows_FileRef="7;#Sandbox/bitest/Lists/Produce/7_.000" ows_MetaInfo="7;#"/>
                </rs:data>
            </listitems></GetListItemsResult></GetListItemsResponse></soap:Body></soap:Envelope>

"

The following routine is designed to iterate through this response and find associated values. It completely fails to execute as though the xData.XML is empty (I also tried xData.xml - no change).

$(xData.XML).find("z\\:row").each(function() {
           quantityOneArray[i] = parseFloat($(this).attr("ows_Quantity"));
           quantityTwoArray[i] = parseFloat($(this).attr("ows_Quantity2"));
           i++;
           alert("xData "+ i + ": "+xData.XML);
    });

Here is a link to a screenshot of my IE8 debugger window with several watches. The xData.xml string is highlighted. This breakpoint immediately precedes my iteration routine above.

  • Is my selector formatted incorrectly?
  • Does my selector require additional escapes?
  • Am I looking at the wrong return value or object?

I am absolutely pulling out my hair here. I changed the selector to "row" in Chrome and it works; in IE8 that makes no difference.

Also, take a look at this blog post to see a working example. Initially I was using "xData.responseXML" but it would show as "Undefined" in the debugger.

回答1:

It looks like you are passing an xml string to $(), which would result in jQuery turning that xml string into HTML. Modern browsers tend to just allow it and go with the "new" html elements, while older browsers (IE6,7,8) choke on them.

Try using

var xmlDoc = $.parseXML(xData.XML);
$(xmlDoc).find("z\\:row").each(...


回答2:

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("msie 8.0")){

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

}else{ // IE8+

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

}