- 論壇徽章:
- 1
|
最近需要在javascript中控制xml數(shù)據(jù),前段時間用dojo,但這玩意是在是太重了,太重的東西有種本能的抗拒
現(xiàn)在又想找點輕型的庫來用,prototype、script.aculo.us和ajaxslt就是我看了半天之后的選擇,這里單說ajaxslt
ajaxslt是google提供的js庫,bsd協(xié)議,可以放心使用,而且google也用在了gmail、google map這些產(chǎn)品上,所以還是很可靠的。唯一不爽的就是資料幾乎沒有,只有幾篇很簡單的文章說一下大概用法,其實這東西也不復雜,但是資料太少不易消除初學的恐懼。
資料少就只好自己讀代碼了,讀代碼的時候看到如下注釋
- // Parses the given XML string with our custom, JavaScript XML parser. Written
- // by Steffen Meschkat (mesch@google.com).
- // For the record: in Safari, we would create native DOM nodes, but
- // in Opera that is not possible, because the DOM only allows HTML
- // element nodes to be created, so we have to do our own DOM nodes.
復制代碼
不得了,google的哥們就是牛啊,原來是完全寫了個js原生的xml實現(xiàn)。
下面就零零碎碎的說一下看到的東西
- function XNode(type, name, opt_value, opt_owner) {
- this.attributes = [];
- this.childNodes = [];
- XNode.init.call(this, type, name, opt_value, opt_owner);
- }
- // Don't call as method, use apply() or call().
- XNode.init = function(type, name, value, owner) {
- this.nodeType = type - 0;
- this.nodeName = '' + name;
- this.nodeValue = '' + value;
- this.ownerDocument = owner;
- this.firstChild = null;
- this.lastChild = null;
- this.nextSibling = null;
- this.previousSibling = null;
- this.parentNode = null;
- }
復制代碼
這個地方用call方法做了一個構(gòu)造函數(shù),從這里可以看出node類型的屬性就是這些了。和平時用的DOM模型不同,這里沒有tagName,代替為nodeName
- function XDocument() {
- // NOTE(mesch): Acocording to the DOM Spec, ownerDocument of a
- // document node is null.
- XNode.call(this, DOM_DOCUMENT_NODE, '#document', null, null);
- this.documentElement = null;
- }
- 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一個
- XNode.prototype.insertAfter = function(newNode, oldNode){
- if (oldNode == newNode){ return; }
- if (newNode.parentNode) {
- newNode.parentNode.removeChild(newNode);
- }
- var newChildren = [];
- for (var i = 0; i < this.childNodes.length; i++){
- var c = this.childNodes[i];
- newChildren.push(c);
- if (c == oldNode) {
- newChildren.push(newNode);
- newNode.parentNode = this;
- newNode.nextSibling = oldNode.nextSibling;
- if (newNode.nextSibling) {
- newNode.nextSibling.previousSibling = newNode;
- }
- oldNode.nextSibling = newNode;
- newNode.previousSibling = oldNode;
- if (this.lastChild == oldNode) {
- this.lastChild = newNode;
- }
- }
- }
- this.childNodes = newChildren;
- }
復制代碼
再來DIY點好用的
- // 返回所有xpath查詢結(jié)果
- XDocument.prototype.selectNodes = function(/* string */xpathString){
- var expr = xpathParse(xpathString);
- var result = expr.evaluate(new ExprContext(this));
- return result.nodeSetValue();
- }
- // 返回第一個xpath查詢結(jié)果
- XDocument.prototype.selectNode = function(/* string */xpathString){
- var result = this.selectNodes(xpathString);
- return result.length ? result[0] : null;
- }
復制代碼
selectNodes()包含了xpath的使用方法,湊合著參考吧
你可以這樣寫
- // 獲取所有require屬性等于1的aaa元素
- var node = xmldoc.selectNodes('//aaa[@require=1]');
- // 獲取第一個require屬性等于1的aaa元素
- 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!!! |
|