jquery - $(xml).find ('someElement' ) : pulling values with jquery from xml with Namespaces

标签 jquery xml internet-explorer firefox 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>

这是 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
}

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

更新:

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

最佳答案

在没有指定命名空间的情况下访问 XML 节点时出现问题。出于某种原因,Chrome 不想看到命名空间。

我发现“b\:DataA”选择器适用于 FireFox 和 IE,而“DataA”选择器适用于 Chrome。

所以...

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

似乎适用于 IE、FireFox 和 Chrome。

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

关于jquery - $(xml).find ('someElement' ) : pulling values with jquery from xml with Namespaces,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10181087/

相关文章:

javascript - 如何在 Yii 中绑定(bind)到 AJAX 加载表单上的 beforeSubmit 事件?

android - Cordova - "config.xml"解析失败

c# - StreamReader 和读取 XML 文件

c# - XmlReader - 我需要编辑一个元素并生成一个新元素

html - 如何选择(和复制)显示 :table with Internet Explorer 8 or 9 中的文本

javascript - 使用 JavaScript 更改 IE 中的背景图像

html - 从移动 View 切换到平板电脑 View 时,如何将 jQuery 移动侧面板从推送更改为覆盖

javascript - 在按下键/单击时缩进文本区域

javascript - 在内容更改时对内联 block 元素进行动画换行

java - 在 Internet Explorer 11 中运行 Java