亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 3305 | 回復: 7
打印 上一主題 下一主題

最近研究ajaxslt的心得 [復制鏈接]

論壇徽章:
1
榮譽版主
日期:2011-11-23 16:44:17
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2007-04-06 10:34 |只看該作者 |倒序瀏覽
最近需要在javascript中控制xml數(shù)據(jù),前段時間用dojo,但這玩意是在是太重了,太重的東西有種本能的抗拒
現(xiàn)在又想找點輕型的庫來用,prototype、script.aculo.us和ajaxslt就是我看了半天之后的選擇,這里單說ajaxslt

ajaxslt是google提供的js庫,bsd協(xié)議,可以放心使用,而且google也用在了gmail、google map這些產(chǎn)品上,所以還是很可靠的。唯一不爽的就是資料幾乎沒有,只有幾篇很簡單的文章說一下大概用法,其實這東西也不復雜,但是資料太少不易消除初學的恐懼。
資料少就只好自己讀代碼了,讀代碼的時候看到如下注釋

  1. // Parses the given XML string with our custom, JavaScript XML parser. Written
  2. // by Steffen Meschkat (mesch@google.com).

  3.   // For the record: in Safari, we would create native DOM nodes, but
  4.   // in Opera that is not possible, because the DOM only allows HTML
  5.   // element nodes to be created, so we have to do our own DOM nodes.
復制代碼

不得了,google的哥們就是牛啊,原來是完全寫了個js原生的xml實現(xiàn)。
下面就零零碎碎的說一下看到的東西

  1. function XNode(type, name, opt_value, opt_owner) {
  2.   this.attributes = [];
  3.   this.childNodes = [];

  4.   XNode.init.call(this, type, name, opt_value, opt_owner);
  5. }

  6. // Don't call as method, use apply() or call().
  7. XNode.init = function(type, name, value, owner) {
  8.   this.nodeType = type - 0;
  9.   this.nodeName = '' + name;
  10.   this.nodeValue = '' + value;
  11.   this.ownerDocument = owner;

  12.   this.firstChild = null;
  13.   this.lastChild = null;
  14.   this.nextSibling = null;
  15.   this.previousSibling = null;
  16.   this.parentNode = null;
  17. }
復制代碼

這個地方用call方法做了一個構(gòu)造函數(shù),從這里可以看出node類型的屬性就是這些了。和平時用的DOM模型不同,這里沒有tagName,代替為nodeName

  1. function XDocument() {
  2.   // NOTE(mesch): Acocording to the DOM Spec, ownerDocument of a
  3.   // document node is null.
  4.   XNode.call(this, DOM_DOCUMENT_NODE, '#document', null, null);
  5.   this.documentElement = null;
  6. }

  7. XDocument.prototype = new XNode(DOM_DOCUMENT_NODE, '#document');
復制代碼

Document也作為一個節(jié)點對待,這邏輯也沒錯,根節(jié)點嘛,整個xml全部抽象成節(jié)點集合,干得漂亮!
沒看過其它DOM實現(xiàn)的代碼是不是這么干的,我是腳本技術(shù)原教旨主義分子,想看也看不懂……

常見的dom api都有了,appendChild() replaceChild() getElementsByTagName() ....
但是沒有insertAfter(),只有insertBefore(),干脆DIY一個

  1. XNode.prototype.insertAfter = function(newNode, oldNode){
  2.     if (oldNode == newNode){ return; }

  3.     if (newNode.parentNode) {
  4.         newNode.parentNode.removeChild(newNode);
  5.     }

  6.     var newChildren = [];
  7.     for (var i = 0; i < this.childNodes.length; i++){
  8.         var c = this.childNodes[i];
  9.         newChildren.push(c);
  10.         if (c == oldNode) {
  11.             newChildren.push(newNode);

  12.             newNode.parentNode = this;

  13.             newNode.nextSibling = oldNode.nextSibling;
  14.             if (newNode.nextSibling) {
  15.                 newNode.nextSibling.previousSibling = newNode;
  16.             }
  17.             oldNode.nextSibling = newNode;
  18.             newNode.previousSibling = oldNode;

  19.             if (this.lastChild == oldNode) {
  20.                 this.lastChild = newNode;
  21.             }
  22.         }
  23.     }
  24.     this.childNodes = newChildren;
  25. }
復制代碼

再來DIY點好用的

  1. // 返回所有xpath查詢結(jié)果
  2. XDocument.prototype.selectNodes = function(/* string */xpathString){
  3.     var expr = xpathParse(xpathString);
  4.     var result = expr.evaluate(new ExprContext(this));
  5.     return result.nodeSetValue();
  6. }

  7. // 返回第一個xpath查詢結(jié)果
  8. XDocument.prototype.selectNode = function(/* string */xpathString){
  9.     var result = this.selectNodes(xpathString);
  10.     return result.length ? result[0] : null;
  11. }
復制代碼

selectNodes()包含了xpath的使用方法,湊合著參考吧
你可以這樣寫

  1. // 獲取所有require屬性等于1的aaa元素
  2. var node = xmldoc.selectNodes('//aaa[@require=1]');
  3. // 獲取第一個require屬性等于1的aaa元素
  4. var node = xmldoc.selectNode('//aaa[@require=1]');
復制代碼


好用的函數(shù),xmlValue()和xmlText()
用xmlValue()獲得元素的文字內(nèi)容,你就不用node.firstChild.nodeValue這么麻煩了,直接xmlValue(node)完事
xmlText()返回xml文本,參數(shù)可以用node,也可以用document

暫時就這么多,ajaxslt感覺還是不錯的,js原生,不用考慮那么多瀏覽器DOM實現(xiàn)差異了
Hell is another browser!!!

論壇徽章:
0
2 [報告]
發(fā)表于 2007-04-06 11:43 |只看該作者
不錯,,為啥不用jquery。。

論壇徽章:
0
3 [報告]
發(fā)表于 2007-04-06 12:07 |只看該作者
這玩意兒現(xiàn)在能在 IE 上用了嗎? 我差不多一年前試過, 那時候只支持 Firefox.

不過 xpath... 還真是不好用, 僅僅是和客戶端交互的話還是 json 吧

論壇徽章:
0
4 [報告]
發(fā)表于 2007-04-06 12:10 |只看該作者
Jquery

論壇徽章:
0
5 [報告]
發(fā)表于 2007-04-06 15:19 |只看該作者
性能如何?

論壇徽章:
1
榮譽版主
日期:2011-11-23 16:44:17
6 [報告]
發(fā)表于 2007-04-06 17:41 |只看該作者
原帖由 lyhiving 于 2007-4-6 12:10 發(fā)表
Jquery

jquery大抵相當于prototype + script.aculo.us
jquery能夠load一段xml,然后返回一個XML Document對象?我大概瀏覽了一遍API,辦不到,更何況ajaxslt還有xlst
jquery能夠操作的dom對象僅限于HTML Document
原帖由 cid73 于 2007-4-6 12:07 發(fā)表
這玩意兒現(xiàn)在能在 IE 上用了嗎? 我差不多一年前試過, 那時候只支持 Firefox.
不過 xpath... 還真是不好用, 僅僅是和客戶端交互的話還是 json 吧

ie上工作正常,暫時沒發(fā)現(xiàn)和firefox有什么區(qū)別
我把xml作為客戶端數(shù)據(jù)庫(姑且這么叫吧)使用,在客戶端上全部做好了才向服務器端一次提交,json貌似和這個主題無關(guān)

性能沒有測試過,感覺gmail都在用,應該可以吧

[ 本帖最后由 夜貓子 于 2007-4-6 17:45 編輯 ]

論壇徽章:
0
7 [報告]
發(fā)表于 2007-04-06 19:32 |只看該作者
原帖由 james.liu 于 2007-4-6 11:43 發(fā)表
不錯,,為啥不用jquery。。


搞的太臃腫了。。

論壇徽章:
0
8 [報告]
發(fā)表于 2007-04-06 19:39 |只看該作者
聽不懂在說什么啊
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP