$(xml).find('someElement'):从名称空间从XML拉jQuery的值 [英] $(xml).find('someElement') : pulling values with jquery from xml with Namespaces

查看:195
本文介绍了$(xml).find('someElement'):从名称空间从XML拉jQuery的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码适用于Chrome,但不适用于IE或FireFox。是否有人知道适当的跨浏览器代码?

 < s:Envelope xmlns:s =http://www.w3 .org / 2003/05 / soap-envelopexmlns:a =http://www.w3.org/2005/08/addressing> 
< s:Header>
< a:Action s:mustUnderstand =1> http://tempuri.org/SubscriptionService/Update< / a:Action>
< netdx:地址> http://docs.oasis-open.org/ws-rx/wsmc/200702/anonymous?id = 4ed8a7ee-b124-e03e-abf0-a294e99cff73< / netdx:地址>
< netdx:SessionId> 177b4f47-5664-d96c-7ffa-0a8d879b67dd< / netdx:SessionId>
< / netdx:Duplex>
< / s:标题>
< s:Body>
< Update xmlns =http://tempuri.org/>
< lstResponseStruct xmlns:b =http://schemas.datacontract.org/2004/07/FSS.Libs.Core.InterprocessData.RMSxmlns:i =http://www.w3.org / 2001 / XMLSchema的实例>
< b:DataA>
< b:ValueA> 1.339565< / b:ValueA>
< b:状态>无< / b:状态>
< / b:DataA>
< b:DataA>
< b:ValueA> 120.3717< / b:ValueA>
< b:状态>无< / b:状态>
< / b:DataA>
< b:DataA>
< b:ValueA> 133.563116< / b:ValueA>
< b:状态>无< / b:状态>
< / b:DataA>
< b:DataA>
< b:ValueA> -0.0059159999999999994< / b:ValueA>
< b:状态>无< / b:状态>
< / b:DataA>
< / lstResponseStruct>
< /更新>
< / s:Body>



以下是JavaScript片段。 ...

 < script src =http://code.jquery.com/jquery.min.jstype = 文本/ JavaScript的 >< /脚本> 

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(*);


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

$ b $(body).find('DataA')。each(function(){
...做某事
}

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



更新:

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

>这是一个访问没有指定名称空间的XML节点的问题。由于某些原因,Chrome浏览器不希望看到名称空间。



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



so ...

  $(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节点查找性能的方法。


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!

解决方案

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.

这篇关于$(xml).find('someElement'):从名称空间从XML拉jQuery的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆