【发布时间】:2012-04-28 04:39:12
【问题描述】:
以下代码适用于 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 sn-ps...
<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!
【问题讨论】:
-
提示:使用 jQuery 的
$.parseXML方法。它返回一个XMLDocument对象(不是 jQuery 对象)。要获取 jQuery 对象,请将返回值包装起来:$($.parseXML('<x>Test</x>')); -
更好的是,因为这似乎是一个 ajax 请求,所以使用 jquery 的
ajax并适当地设置dataType(或者让 jquery 弄清楚),你甚至不需要调用 @ 987654329@直接。 -
$(xData.responseXML).find("z\\:row, row").each(function() { // 做事 });似乎适用于 IE、FireFox 和 Chrome。
标签: jquery xml internet-explorer firefox google-chrome