$(xml).find('someElement'):使用带有命名空间的xml从jquery中提取值

ant*_*pes 6 xml firefox jquery internet-explorer google-chrome

以下代码适用于Chrome,但不适用于IE或FireFox.有人知道相应的跨浏览器代码吗?

<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>
Run Code Online (Sandbox Code Playgroud)

以下是JavaScript代码段...

<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
}
Run Code Online (Sandbox Code Playgroud)

由于某种原因,在每个浏览器中"body"肯定包含body xml,但$(body).find('DataA')不返回IE或FireFox的结果.

更新:

添加命名空间$(body).find('b \\:DataA')适用于FireFox和IE,但会破坏Chrome!

ant*_*pes 12

在没有指定命名空间的情况下访问XML节点是一个问题.出于某种原因,Chrome不希望看到命名空间.

我发现"b \:DataA"选择器适用于FireFox和IE,而"DataA"选择器适用于Chrome.

所以...

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

似乎适用于IE,FireFox和Chrome.

有关提高XML节点查找性能的更多信息和方法,请参阅http://www.steveworkman.com/html5-2/javascript/2011/improving-javascript-xml-node-finding-performance-by-2000/.