最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在jquery中處理帶有命名空間的XML數(shù)據(jù)

 更新時(shí)間:2011年06月13日 23:55:41   作者:  
如果你在做AJAX應(yīng)用,則你可能經(jīng)常會(huì)用到j(luò)query(或者其他框架)處理服務(wù)返回的數(shù)據(jù)。如果用Jquery處理Json格式,將是相當(dāng)方便的。
但不幸的是,很多服務(wù)返回的數(shù)據(jù)仍然是XML格式的。
jquery對(duì)于xml這種數(shù)據(jù)的處理是內(nèi)置支持的,這一點(diǎn)沒有任何問題。但前提是返回的數(shù)據(jù)沒有帶任何命名空間。例如下面這份數(shù)據(jù)
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8" ?>
<data>
<Employee id="1" firstName="ares" lastName="chen"></Employee>
<Employee id="1" firstName="ares" lastName="chen"></Employee>
<Employee id="1" firstName="ares" lastName="chen"></Employee>
<Employee id="1" firstName="ares" lastName="chen"></Employee>
<Employee id="1" firstName="ares" lastName="chen"></Employee>
<Employee id="1" firstName="ares" lastName="chen"></Employee>
<Employee id="1" firstName="ares" lastName="chen"></Employee>
<Employee id="1" firstName="ares" lastName="chen"></Employee>
</data>

要處理這樣的數(shù)據(jù),jquery代碼大致如下
復(fù)制代碼 代碼如下:

var div = $("#placeholder");
// 處理不帶命名空間的xml
$.get("data.xml", null, function (data) {
var employees = $("Employee", data); //找到所有的Employee節(jié)點(diǎn)
var ul = $("<ul />");
employees.each(function () {
$("<li />").text($(this).attr("firstName") + " " + $(this).attr("lastName")).appendTo(ul);// 將每一行數(shù)據(jù)構(gòu)造一個(gè)新的li標(biāo)簽,并且將其插入到ul中
});
ul.appendTo(div);
});

但如果我們的XML數(shù)據(jù)帶有命名空間,則上述代碼就會(huì)無效。原因是因?yàn)閖query默認(rèn)處理不了命名空間\
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8" ?>
<data xmlns:d="http://tech.xizhang.com">
<d:Employee id="1" firstName="bill" lastName="gates"></d:Employee>
<d:Employee id="1" firstName="bill" lastName="gates"></d:Employee>
<d:Employee id="1" firstName="bill" lastName="gates"></d:Employee>
<d:Employee id="1" firstName="bill" lastName="gates"></d:Employee>
<d:Employee id="1" firstName="bill" lastName="gates"></d:Employee>
<d:Employee id="1" firstName="bill" lastName="gates"></d:Employee>
<d:Employee id="1" firstName="bill" lastName="gates"></d:Employee>
<d:Employee id="1" firstName="bill" lastName="gates"></d:Employee>
<d:Employee id="1" firstName="bill" lastName="gates"></d:Employee>
<d:Employee id="1" firstName="bill" lastName="gates"></d:Employee>
<d:Employee id="1" firstName="bill" lastName="gates"></d:Employee>
<d:Employee id="1" firstName="bill" lastName="gates"></d:Employee>
</data>

為了解決這個(gè)問題,有熱心的網(wǎng)友,編寫了一個(gè)jquery插件,叫做jquery.xmlns.js,有興趣可以通過下面了解和下載
http://www.rfk.id.au/blog/entry/xmlns-selectors-jquery/
那么,我們可以用如下的方法來解決問題
復(fù)制代碼 代碼如下:

$.xmlns["d"] = "http://tech.xizhang.com";
// 處理帶命名空間的xml
$.get("datawithnamespace.xml", null, function (data) {
var employees = $("d|Employee", data); //找到所有的Employee節(jié)點(diǎn)
var ul = $("<ul />");
employees.each(function () {
$("<li />").text($(this).attr("firstName") + " " + $(this).attr("lastName")).appendTo(ul);
});
ul.appendTo(div);
});

不得不說,XML這個(gè)技術(shù)規(guī)范中的命名空間真是一個(gè)很不好的設(shè)計(jì)。增加了很多麻煩,勝過于它帶來的好處。
本文的例子完整代碼如下
復(fù)制代碼 代碼如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.xmlns.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(function () {
var div = $("#placeholder");
// 處理不帶命名空間的xml
$.get("data.xml", null, function (data) {
var employees = $("Employee", data); //找到所有的Employee節(jié)點(diǎn)
var ul = $("<ul />");
employees.each(function () {
$("<li />").text($(this).attr("firstName") + " " + $(this).attr("lastName")).appendTo(ul);// 將每一行數(shù)據(jù)構(gòu)造一個(gè)新的li標(biāo)簽,并且將其插入到ul中
});
ul.appendTo(div);
});
$("<br />").appendTo(div);
$.xmlns["d"] = "http://tech.xizhang.com";
// 處理帶命名空間的xml
$.get("datawithnamespace.xml", null, function (data) {
var employees = $("d|Employee", data); //找到所有的Employee節(jié)點(diǎn)
var ul = $("<ul />");
employees.each(function () {
$("<li />").text($(this).attr("firstName") + " " + $(this).attr("lastName")).appendTo(ul);
});
ul.appendTo(div);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="placeholder">
</div>
</form>
</body>
</html>

最后,在瀏覽器中看到的效果如下。有圖有真相

image

相關(guān)文章

最新評(píng)論

贞丰县| 香港| 麻阳| 额尔古纳市| 汉阴县| 岳阳市| 浪卡子县| 泸溪县| 军事| 正镶白旗| 临武县| 昌乐县| 尚志市| 江津市| 扶风县| 和平区| 郸城县| 栖霞市| 南城县| 湛江市| 仙桃市| 盘锦市| 武安市| 阳江市| 九寨沟县| 澎湖县| 南雄市| 夏津县| 金门县| 灯塔市| 巴东县| 吉木萨尔县| 伊宁县| 黔西县| 嘉黎县| 共和县| 洮南市| 岚皋县| 湘阴县| 拉萨市| 临汾市|