Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

meet nodejs and support doctype parsing

  • Loading branch information...
commit 79055697a69345f8e9b66b78f21fe50f561117de 1 parent 4da7ce0
廖健 authored
117 README.md
Source Rendered
... ... @@ -1,92 +1,97 @@
1 1 jsxml
2 2 ==============
3 3
4   -jsxml is an XML library for javascript
  4 +jsxml is an XML library for javascript (and node)
5 5
6 6 How to use?
7 7 ------------
8 8 After add this library to your project, there will be a global object named jsxml.
9   -
10   - var Namespace = jsxml.Namespace,
11   - QName = jsxml.QName,
12   - XML = jsxml.XML,
13   - XMLList = jsxml.XMLList;
14   -
  9 +```javascript
  10 +var Namespace = jsxml.Namespace,
  11 + QName = jsxml.QName,
  12 + XML = jsxml.XML,
  13 + XMLList = jsxml.XMLList;
  14 +```
15 15 Here you go:
  16 +```javascript
  17 +var xml = new XML("your xml string");
16 18
17   - var xml = new XML("your xml string");
18   -
19   - //find child nodes
20   - var child = xml.child('childTag');
  19 +//find child nodes
  20 +var child = xml.child('childTag');
21 21
22   - //find descendants nodes
23   - var descendants = xml.descendants('descendantTag');
  22 +//find descendants nodes
  23 +var descendants = xml.descendants('descendantTag');
24 24
25   - //get all children
26   - var children = xml.children();
27   - //or
28   - var children = xml.child('*');
  25 +//get all children
  26 +var children = xml.children();
  27 +//or
  28 +var children = xml.child('*');
29 29
30   - //get text node
31   - var text = xml.text();
  30 +//get text node
  31 +var text = xml.text();
32 32
33   - //get element node
34   - var elements = xml.elements();
  33 +//get element node
  34 +var elements = xml.elements();
35 35
36   - //get comment node
37   - var comments = xml.comments();
  36 +//get comment node
  37 +var comments = xml.comments();
38 38
39   - //get attribute
40   - var attribute = xml.attribute("attributeName");
  39 +//get attribute
  40 +var attribute = xml.attribute("attributeName");
41 41
42   - //get all attributes
43   - var attributes = xml.attributes();
  42 +//get all attributes
  43 +var attributes = xml.attributes();
  44 +```
44 45
45 46 All methods above return an XML object or XMLList object, if you want to get the String type content, you should:
  47 +```javascript
  48 +var xml = new XML("your xml string");
46 49
47   - var xml = new XML("your xml string");
48   -
49   - var attrValue = xml.attribute('attrName').toString();
50   - //or
51   - var attrValue = xml.attribute('attrName').getValue();
52   -
53   - var childA = xml.child('a').getValue();
54   - //or
55   - var childA = xml.child('a').getValue();
  50 +var attrValue = xml.attribute('attrName').toString();
  51 +//or
  52 +var attrValue = xml.attribute('attrName').getValue();
56 53
  54 +var childA = xml.child('a').toString();
  55 +//or
  56 +var childA = xml.child('a').getValue();
  57 +```
57 58
58 59 If you want to modify the value, you should call method setValue:
  60 +```javascript
  61 +var xml = new XML("your xml string");
59 62
60   - var xml = new XML("your xml string");
  63 +var attr= xml.attribute('attrName');
  64 +attr.setValue("newValue");
61 65
62   - var attr= xml.attribute('attrName');
63   - attr.setValue("newValue");
64   -
65   - var childA = xml.child('a');
66   - childA.setValue("newValue");
  66 +var childA = xml.child('a');
  67 +childA.setValue("newValue");
  68 +```
67 69
68 70 You can regenerate the XML
69   -
70   - var str = xml.toXMLString();
  71 +```javascript
  72 +var str = xml.toXMLString();
  73 +```
71 74
72 75 While dealing with a list of childs in XML tree, you should use XMLList API:
73   -
74   - var list = xml.child("item");
75   - list.each(function(item, index){
76   - //item is an XML
77   - });
  76 +```javascript
  77 +var list = xml.child("item");
  78 +list.each(function(item, index){
  79 +//item is an XML
  80 +});
  81 +```
78 82
79 83 Advanced topics
80 84 ----------------
81 85
82 86 You can also add, retrieve or remove namespaces:
83   -
84   - var xml = new XML("your xml string");
85   - var ns = xml.namespace("prefix");
86   -
87   - var nsNew = new Namespace("prefix", 'uri');
88   - xml.addNamespace(nsNew);
89   - xml.removeNamespace(nsNew);
  87 +```javascript
  88 +var xml = new XML("your xml string");
  89 +var ns = xml.namespace("prefix");
  90 +
  91 +var nsNew = new Namespace("prefix", 'uri');
  92 +xml.addNamespace(nsNew);
  93 +xml.removeNamespace(nsNew);
  94 +```
90 95
91 96 Bugs & Feedback
92 97 ----------------
6 build/jsxml-0.1.0-yui.js
... ... @@ -1,6 +0,0 @@
1   -/*
2   - * Copyright 2011 http://colorhook.com.
3   - * @author: <a href="colorhook@gmail.com">colorhook</a>
4   - * @version:0.1.0
5   - */
6   -var jsxml=(function(){var n=/^<([a-zA-Z\$_]+:{0,1}[a-zA-Z0-9\$\-_]*)((?:\s+[a-zA-Z\$_]+:{0,1}[a-zA-Z0-9\$\-_]*(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/,f=/^<\/([a-zA-Z0-9\$\-_:]+)[^>]*>/,j=/([a-zA-Z\$_]+:{0,1}[a-zA-Z0-9\$\-_]*)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))?/g,b,c,m,o,g,e,h,d,l,i,k,a;c=String.prototype.trim?function(p){return p&&p.trim?p.trim():p}:function(p){try{return p.replace(/^\s+|\s+$/,"")}catch(q){return p}};m=function(r,p){for(var q in p){if(p.hasOwnProperty(q)){r[q]=p[q]}}if(!-[1,]){if(p.toString!==Object.prototype){r.toString=p.toString}}};arrayIndexOf=function(s,r){if(s.indexOf){return s.indexOf(r)}for(var q=0,p=s.length;q<p;q++){if(r===s[q]){return q}}return -1};o=function(p){return p.match(/&(amp|lt|gt);/)};g=function(p){p=p.replace(/&[^(amp;|lt;|gt;)]/g,"&amp;");p=p.replace(/</g,"&lt;");p=p.replace(/>/g,"&gt;");return p};e=function(p){p=p.replace(/&lt;/g,"<");p=p.replace(/&gt;/g,">");p=p.replace(/&amp;/g,"&");return p};b=function(q,y){var t,u,r,v=[],w=q;v.last=function(){return this[this.length-1]};while(q){u=true;if(q.indexOf("<![CDATA[")==0){t=q.indexOf("]]>");if(t>0){x=q.substring(9,t);if(y.chars){y.chars(x,true)}q=q.substring(t+3)}u=false}else{if(q.indexOf("<?")==0){t=q.indexOf("?>");if(t>0){x=q.substr(2,t-2);if(y.instruction){y.instruction(x)}q=q.substring(t+2)}u=false}else{if(q.indexOf("<!--")==0){t=q.indexOf("-->");if(t>=0){if(y.comment){y.comment(q.substring(4,t))}q=q.substring(t+3);u=false}}else{if(q.indexOf("</")==0){r=q.match(f);if(r){q=q.substring(r[0].length);r[0].replace(f,s);u=false}}else{if(q.indexOf("<")==0){r=q.match(n);if(r){q=q.substring(r[0].length);r[0].replace(n,p);u=false}}}}}}if(u){t=q.indexOf("<");var x=t<0?q:q.substring(0,t);q=t<0?"":q.substring(t);if(y.chars){y.chars(x)}}if(q==w){throw"Parse Error: "+q}w=q}s();function p(z,C,D,A){A=!!A;if(!A){v.push(C)}if(y.start){var B=[];D.replace(j,function(F,E){var G=arguments[2]?arguments[2]:arguments[3]?arguments[3]:arguments[4]?arguments[4]:E?E:"";B.push({name:E,value:G,escaped:G.replace(/(^|[^\\])"/g,'$1\\"')})});if(y.start){y.start(C,B,A)}}}function s(z,B){if(!B){var C=0}else{for(var C=v.length-1;C>=0;C--){if(v[C]==B){break}}}if(C>=0){for(var A=v.length-1;A>=C;A--){if(y.end){y.end(v[A])}}v.length=C}}};h=function(r,q){var p=arguments.length;if(p>=2){this.prefix=String(r);this.uri=String(q)}else{if(p==1){this.prefix="";this.uri=String(r)}else{this.prefix="";this.uri=""}}};h.prototype={constructor:h,toString:function(){return this.uri},copy:function(){var p=new h();p.prefix=this.prefix;p.uri=this.uri;return p},equals:function(p){return this.prefix===p.prefix&&this.uri===p.uri}};d=function(r,q){var p=arguments.length;if(p>=2){this.uri=String(r);this._ns=(r&&r.constructor==h)?r:new h(r);this.localName=String(q)}else{if(p==1){this.uri="";this._ns=new h();this.localName=String(r)}else{this.uri="";this._ns=new h();this.localName=""}}};d.prototype={constructor:d,toString:function(){var p=this.uri?this.uri+"::":"";return p+this.localName},copy:function(){var p=new d();p.uri=this.uri;p.localName=this.localName;p._ns=this._ns.copy();return p},equals:function(p){return this.localName==p.localName&&this._ns.equals(p._ns)}};d._format=function(r,t){var q=r.split(":"),s,p;if(q.length==2){s=q[0];p=q[1]}else{s="";p=r}return new d(new h(s,t),p)};l={ELEMENT:"element",COMMENT:"comment",PROCESSING_INSTRUCTIONS:"processing-instructions",TEXT:"text",ATTRIBUTE:"attribute"};i=function(p){if(p._list&&p.length()==1){return i(p._list[0])}return p};k=function(){this._list=[]};m(k.prototype,{constructor:k,_addXML:function(p){if(p.constructor==a){this._list.push(p)}else{if(p.constructor==k){this._list=this._list.concat(p._list)}}return this},length:function(){return this._list.length},childIndex:function(){throw new Error("this method only availabe in single list XML")},child:function(r){var q=new k();this.each(function(p){var s=p.child(r);if(s.length()){q._addXML(s)}});return i(q)},children:function(){return this.child("*")},attribute:function(r){var q=new k();this.each(function(p){q._addXML(p.attribute(r))});return i(q)},attributes:function(q){return this.attribute("*")},elements:function(r){var q=new k();this.each(function(p){q._addXML(p.elements(r))});return i(q)},descendants:function(r){var q=new k();this.each(function(p){q._addXML(p.descendants(r))});return i(q)},normalize:function(){this.each(function(p){p.normalize()})},hasSimpleContent:function(){for(var q=0,p=this._list.length;q<p;q++){var r=this._list[q];if(r.constructor==k||r.hasComplexContent()){return false}}return true},hasComplexContent:function(){return !this.hasSimpleContent()},text:function(){var p=new k();this.each(function(r){if(r.constructor==a){var q=r.text();if(q._text!=""){p._addXML(q)}}});return i(p)},comments:function(){var p=new k();this.each(function(q){if(q.constructor==a){p._addXML(q.comments())}});return i(p)},processingInstructions:function(){var p=new k();this.each(function(q){if(q.constructor==a){p._addXML(q.processingInstructions())}});return i(p)},copy:function(){var p=new k();this.each(function(r,q){p._list[q]=r.copy()});return p},toXMLString:function(){var p=[];this.each(function(q){p.push(q.toXMLString())});return p.join("\n")},toString:function(){var p="";this.each(function(q){p+=q.toString()});return p},item:function(p){return this._list[p]},each:function(r){for(var q=0,p=this._list.length;q<p;q++){r(this._list[q],q,this)}}});a=function(r){this._children=[];this._attributes=[];this._namespaces=[];this._nodeKind=l.ELEMENT;this._qname=null;this._parent=null;this._text=null;this._useCDATA=false;var q,p=this;if(r){b(r,{start:function(A,z,s){var v;if(!q){v=p}else{v=new a();v._parent=q}v._qname=d._format(A);for(var u in z){var x=new a();x._nodeKind=l.ATTRIBUTE;var t;if(z[u].name==="xmlns"){t=new d(new h("xmlns",z[u].value),"")}else{t=d._format(z[u].name,z[u].value)}var w=t._ns.prefix||"";if(w==="xmlns"){var y=new h(t.localName,t.uri);v.addNamespace(y);if(t.localName==v._qname._ns.prefix){v.setNamespace(y)}}else{x._qname=t;x._text=z[u].value;v._attributes.push(x)}}q=v;if(s){this.end(A)}},chars:function(u,s){u=c(u);if(u==""&&a.ignoreWhitespace){return}var t=new a();t._nodeKind=l.TEXT;t._text=u;t._useCDATA=s;q._children.push(t)},end:function(s){if(q&&q._parent){q._parent._children.push(q);q=q._parent}else{if(q==p){q=null}}},comment:function(t){var s=new a();s._nodeKind=l.COMMENT;s._text=t;q&&q._children.push(s)},instruction:function(t){var s=new a();s._nodeKind=l.PROCESSING_INSTRUCTIONS;s._text=t;q&&q._children.push(s)}})}};m(a.prototype,{addNamespace:function(p){if(p.prefix!=undefined){this.removeNamespace(p);this._namespaces.push(p)}},removeNamespace:function(r){for(var q=0,p=this._namespaces.length;q<p;q++){if(r.prefix==this._namespaces[q].prefix){this._namespaces.splice(q,1);break}}},namespace:function(r){if(!r){return new h()}for(var q=0,p=this._namespaces.length;q<p;q++){if(r==this._namespaces[q].prefix){return this._namespaces[q]}}return undefined},setNamespace:function(p){if(p&&p.constructor==h){this.addNamespace(p);if(this._qname){this._qname.uri=p.uri;this._qname._ns=p}}},namespaceDeclarations:function(){return this._namespaces},inScopeNamespaces:function(){var q=this._namespaces;var p=this._parent;while(p){q=p.inScopeNamespaces().concat(q);p=p._parent}return q},nodeKind:function(){return this._nodeKind},name:function(){if(!this._qname){return null}if(this._qname.uri){return this._qname.uri+":"+this._qname.localName}return this._qname.localName},localName:function(){if(!this._qname){return null}return this._qname.localName},setName:function(p){if(this._qname==null){return}if(/^[a-zA-Z]+[a-zA-Z0-9]*$/.test(p)){this._qname.uri="";this._qname.localName=p}else{throw new Error("invalid value for XML name")}},setLocalName:function(p){if(this._qname==null){return}if(/^[a-zA-Z\$_]+[a-zA-Z0-9\$\-_]*$/.test(p)){this._qname.localName=p}else{throw new Error("invalid value for XML localName")}},length:function(){return 1},attribute:function(v){var r=this._attributes,s,q,t,u=new k();for(s=0,q=r.length;s<q;s++){t=r[s];if(t._qname.localName==v||v=="*"){u._addXML(t)}}return i(u)},attributes:function(){return this.attribute("*")},_createTextNode:function(q){var p=new a();p._nodeKind=l.TEXT;p._text=q;p._useCDATA=/['"<>&]/.test(q);return p},appendChild:function(r){var q=r.constructor;if(q==a){r._parent=this;this._children.push(r)}else{if(q==k){r.each(function(s){s._parent=this});this._children=this._children.concat(r._list)}else{if(q==String){var p=this._createTextNode(r);p._parent=this;this._children.push(p)}}}return this},prependChild:function(r){var q=r.constructor;if(q==a){r._parent=this;this._children.unshift(r)}else{if(q==k){r.each(function(s){s._parent=this});this._children=this._list.concat(this._children)}else{if(q===String){var p=this._createTextNode(r);p._parent=this;this._children.unshift(p)}}}return this},normalize:function(){var s,p,t,r=this._children,v=[];for(s=0,p=r.length;s<p;s++){var u=r[s],q=u.nodeKind();if(q==l.TEXT){if(t){u._text=t._text+u._text;r[s-1]=null}t=u}else{if(q==l.ELEMENT){u.normalize();t=null}}}for(s=0,p=r.length;s<p;s++){r[s]&&v.push(r[s])}this._children=v},_getFilterChildren:function(){var s,p,u=[],r=this._children;for(s=0,p=r.length;s<p;s++){var t=r[s],q=t.nodeKind();if(q==l.ELEMENT||q==l.TEXT||(q==l.COMMENT&&!a.ignoreComments)||(q==l.PROCESSING_INSTRUCTIONS&&!a.ignoreProcessingInstructions)){u.push(t)}}return u},child:function(u){var t=new k(),s,q,v=this._getFilterChildren();if(typeof u=="number"){if(v.length!=0&&v[u]){t._addXML(v[u])}}else{for(s=0,q=v.length;s<q;s++){var r=v[s];if(r.localName()==u||u=="*"){t._addXML(r)}}}return i(t)},childIndex:function(q){if(this._parent){return arrayIndexOf(this._parent._getFilterChildren(),this)}return -1},children:function(){return this.child("*")},setChildren:function(p){this._children=[];return this.appendChild(p)},replace:function(y,x){var z=-1,u,s,w=this._children,q=[];for(u=0,s=w.length;u<s;u++){var v=w[u],p=v.nodeKind();if((v.localName()==y||y=="*")&&p==l.ELEMENT){if(z==-1){z=u;var r=x.constructor;if(r==a){x._parent=this;q.push(x)}else{if(r==k){x.each(function(t){t._parent=this});q=q.concat(x._list)}else{if(r===String){var A=this._createTextNode(x);A._parent=this;q.push(A)}}}}}else{q.push(v)}}if(z!=-1){this._children=q;this.normalize()}return this},elements:function(u){if(arguments.length==0){u="*"}var t=new k(),s,q,v=this._children;for(s=0,q=v.length;s<q;s++){var r=v[s];if((r.localName()==u||u=="*")&&r.nodeKind()==l.ELEMENT){t._addXML(r)}}return i(t)},descendants:function(v){if(arguments.length==0){v="*"}var u=new k(),t,q,w=this._children;for(t=0,q=w.length;t<q;t++){var s=w[t],r=s.nodeKind();if((s.localName()==v||v=="*")&&(r==l.ELEMENT||r==l.TEXT)){u._addXML(s)}if(s._nodeKind==l.ELEMENT){u._addXML(s.descendants(v))}}return i(u)},insertChildBefore:function(s,r){if(s==null){return this.appendChild(r)}if(s.constructor!=a){return undefined}var t=s.childIndex();if(s._parent==this&&t!=-1){if(r.constructor==a){r._parent=this;this._children.splice(t,0,r)}else{if(r.constructor==k){for(var q=0,p=r._list.length;q<p;q++){r._list[q]._parent=this;this._children.splice(t+q,0,r._list[q])}}else{return undefined}}return this}else{return undefined}},insertChildAfter:function(s,r){if(s==null){return this.prependChild(r)}if(s.constructor!=a){return undefined}var t=s.childIndex();if(s._parent==this&&t!=-1){if(r.constructor==a){r._parent=this;this._children.splice(t+1,0,r)}else{if(r.constructor==k){for(var q=0,p=r._list.length;q<p;q++){r._list[q]._parent=this;this._children.splice(t+1+q,0,r._list[q])}}else{return undefined}}return this}else{return undefined}},parent:function(){return this._parent},hasSimpleContent:function(){var s=this._children;for(var r=0,p=s.length;r<p;r++){var q=s[r].nodeKind();if(q===l.ELEMENT){return false}}return true},hasComplexContent:function(){return !this.hasSimpleContent()},comments:function(){var s=new k(),r,p,t=this._getFilterChildren();for(r=0,p=t.length;r<p;r++){var q=t[r];if(q.nodeKind()==l.COMMENT){s._addXML(q)}}return i(s)},text:function(){var u=this._children,t="";for(var s=0,p=u.length;s<p;s++){var q=u[s].nodeKind();if(q===l.TEXT){if(u[s]._useCDATA){t+=u[s]._text}else{t+=e(u[s]._text)}}}return this._createTextNode(t)},comments:function(){var s=new k(),r,p,t=this._getFilterChildren();for(r=0,p=t.length;r<p;r++){var q=t[r];if(q.nodeKind&&q.nodeKind()==l.COMMENT){s._addXML(q)}}return i(s)},processingInstructions:function(){var s=new k(),r,p,t=this._getFilterChildren();for(r=0,p=t.length;r<p;r++){var q=t[r];if(q.nodeKind&&q.nodeKind()==l.PROCESSING_INSTRUCTIONS){s._addXML(q)}}return i(s)},copy:function(){var q=new a(),r,p;q._nodeKind=this._nodeKind;q._text=this._text;q._useCDATA=this._useCDATA;if(this._qname){q._qname=this._qname.copy()}for(r=0,p=this._namespaces.length;r<p;r++){q._namespaces[r]=this._namespaces[r].copy()}for(r=0,p=this._attributes.length;r<p;r++){q._attributes[r]=this._attributes[r].copy()}for(r=0,p=this._children.length;r<p;r++){q._children[r]=this._children[r].copy()}return q},_toXMLString:function(x,C){var G="",H,A,y,t=this._nodeKind,D=C?this.inScopeNamespaces():this._namespaces,F=this._attributes,w=this._children,z=a.prettyPrinting,u=[];x=x||0;if(z){for(A=0;A<x;A++){G+=" "}}if(t==l.ATTRIBUTE){return G+this._text}else{if(t==l.TEXT){if(this._useCDATA){return G+"<![CDATA["+this._text+"]]>"}else{return G+g(this._text)}}else{if(t==l.COMMENT){return G+"<!--"+this._text+"-->"}else{if(t==l.PROCESSING_INSTRUCTIONS){return G+"<?"+this._text+"?>"}}}}if(this._qname._ns.prefix){H=this._qname._ns.prefix+":"+this.localName()}else{H=this.localName()}G+="<"+H;for(A=0,y=D.length;A<y;A++){var B=D[A].prefix?"xmlns:"+D[A].prefix:"xmlns";u.push({label:B,value:D[A].uri})}for(A=0,y=F.length;A<y;A++){var r=F[A]._qname,B=r._ns.prefix,E;if(B){E=B+":"+r.localName}else{E=r.localName}u.push({label:E,value:F[A]._text})}if(u.length>0){for(A=0,y=u.length;A<y;A++){G+=" "+u[A].label+'="'+u[A].value+'"'}}u=[];for(A=0,y=w.length;A<y;A++){var v=w[A];if(v.nodeKind()==l.ELEMENT){u.push(v)}else{if(v.nodeKind()==l.COMMENT&&!a.ignoreComments){u.push(v)}else{if(v.nodeKind()==l.PROCESSING_INSTRUCTIONS&&!a.ignoreProcessingInstructions){u.push(v)}else{if(v.nodeKind()==l.TEXT){u.push(v)}}}}}if(u.length==0){G+="/>"}else{if(u.length==1&&u[0].nodeKind()==l.TEXT){G+=">";G+=u[0]._toXMLString(0);G+="</"+H+">"}else{G+=">";for(A=0,y=u.length;A<y;A++){if(z){G+="\n"}G+=u[A]._toXMLString(x+a.prettyIndent)}if(z){G+="\n";for(A=0;A<x;A++){G+=" "}}G+="</"+H+">"}}return G},toXMLString:function(){return this._toXMLString(0,true)},toString:function(){if(this.hasComplexContent()){return this.toXMLString()}if(this.nodeKind()==l.TEXT||this.nodeKind()==l.ATTRIBUTE){return this._text}var t="";for(var q=0,p=this._children.length;q<p;q++){var r=this._children[q];if(r._nodeKind==l.TEXT){if(r._useCDATA){t+=r._text}else{t+=e(r._text)}}}return t},getValue:function(){var p=this._nodeKind;if(p==l.TEXT){if(!this._useCDATA&&o(this._text)){return e(this._text)}return this._text}else{if(p==l.ATTRIBUTE){return this._text}else{if(p==l.ELEMENT&&this.hasSimpleContent()){var q=this.text();if(q.getValue){return q.getValue()}}}}return undefined},setValue:function(u){var q=this._nodeKind;if(q==l.TEXT||q==l.ATTRIBUTE||q==l.COMMENT||q==l.PROCESSING_INSTRUCTIONS){this._text=u}else{if(q==l.ELEMENT&&this.hasSimpleContent()){var v=[],t=this._createTextNode(u);t._parent=this;v.push(t);for(var r=0,p=this._children.length;r<p;r++){var s=this._children[r];if(s._nodeKind!=l.TEXT){v.push(s)}}this._children=v}}return this}});m(a,{ignoreComments:true,ignoreProcessingInstructions:true,ignoreWhitespace:true,prettyIndent:2,prettyPrinting:true,settings:function(){return{ignoreComments:this.ignoreComments,ignoreProcessingInstructions:this.ignoreProcessingInstructions,ignoreWhitespace:this.ignoreWhitespace,prettyIndent:this.prettyIndent,prettyPrinting:this.prettyPrinting}},setSettings:function(q){if(!q){return}var p=function(r){if(q.hasOwnProperty(r)){a[r]=q[r]}};p("ignoreComments");p("ignoreProcessingInstructions");p("ignoreWhitespace");p("prettyIndent");p("prettyPrinting")}});return{containsEntity:o,replaceToEntity:g,replaceFromEntity:e,parseXML:b,Namespace:h,QName:d,NodeKind:l,XMLList:k,XML:a}})();
1,647 build/jsxml-0.1.0.js
... ... @@ -1,1647 +0,0 @@
1   -/*!
2   - * Copyright 2011 http://colorhook.com.
3   - * @author: <a href="colorhook@gmail.com">colorhook</a>
4   - * @version:0.1.0
5   - */
6   -/**
7   - * @preserve Copyright 2011 http://colorhook.com.
8   - * @author: <a href="colorhook@gmail.com">colorhook</a>
9   - * @version:0.1.0
10   - */
11   -var jsxml = (function(){
12   -
13   - /**
14   - * XML parser comes from HTML Parser by John Resig (ejohn.org)
15   - * http://ejohn.org/files/htmlparser.js
16   - * Original code by Erik Arvidsson, Mozilla Public License
17   - * http://erik.eae.net/simplehtmlparser/simplehtmlparser.js
18   - */
19   - // Regular Expressions for parsing tags and attributes
20   - var startTag = /^<([a-zA-Z\$_]+:{0,1}[a-zA-Z0-9\$\-_]*)((?:\s+[a-zA-Z\$_]+:{0,1}[a-zA-Z0-9\$\-_]*(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/,
21   - endTag = /^<\/([a-zA-Z0-9\$\-_:]+)[^>]*>/,
22   - attr = /([a-zA-Z\$_]+:{0,1}[a-zA-Z0-9\$\-_]*)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))?/g,
23   - _parseXML,
24   - trim,
25   - merge,
26   - containsEntity,
27   - replaceToEntity,
28   - replaceFromEntity,
29   - Namespace,
30   - QName,
31   - NodeKind,
32   - _getXMLIfLengthEqualOne,
33   - XMLList,
34   - XML;
35   -
36   - /**
37   - * @description trim whitespace before or after the String
38   - * @param s{String}
39   - * @return String
40   - */
41   - trim = String.prototype.trim ? function(s){
42   - return s && s.trim ? s.trim() : s;
43   - }: function(s){
44   - try{
45   - return s.replace(/^\s+|\s+$/, '');
46   - }catch(err){
47   - return s;
48   - }
49   - };
50   - /**
51   - * @description merge one object to another, used to extend object.
52   - * because of the toString property is not enumerable in IE browser,
53   - * so we must assign it explicity.
54   - * @param s1{Object} object need to be extended.
55   - * @param s2{Object} extension object
56   - * @return Object
57   - */
58   - merge = function(s1, s2){
59   - for(var i in s2){
60   - if(s2.hasOwnProperty(i)){
61   - s1[i] = s2[i];
62   - }
63   - }
64   - //fix IE toString() not Enumerable bug
65   - if(!-[1,]){
66   - if(s2.toString !== Object.prototype){
67   - s1.toString = s2.toString;
68   - }
69   - }
70   - };
71   - /**
72   - * @description find element's index from an array.
73   - * @param array{Array} the array hosting the element
74   - * @param ele{Object} the element in the array
75   - * @return Number
76   - */
77   - arrayIndexOf = function(array, ele){
78   - if(array.indexOf){
79   - return array.indexOf(ele);
80   - }
81   - for(var i = 0, l = array.length; i < l; i++){
82   - if(ele === array[i]){
83   - return i;
84   - }
85   - }
86   - return -1;
87   - };
88   - /**
89   - * @description check if the string contains XML entity format(&amp; &lt; &gt;) or not.
90   - * @param str{String}
91   - * @return Boolean
92   - */
93   - containsEntity = function(str){
94   - return str.match(/&(amp|lt|gt);/);
95   - };
96   - /**
97   - * @description convert special characters (& < >) to XML entity format.
98   - * @param str{String}
99   - * @return String
100   - */
101   - replaceToEntity = function(str){
102   - str = str.replace(/&[^(amp;|lt;|gt;)]/g, '&amp;');
103   - str = str.replace(/</g, '&lt;');
104   - str = str.replace(/>/g, '&gt;');
105   - return str;
106   - }
107   - /**
108   - * @description convert from XML entity format to readable characters.
109   - * @param str{String}
110   - * @return String
111   - */
112   - replaceFromEntity = function(str){
113   - str = str.replace(/&lt;/g, '<');
114   - str = str.replace(/&gt;/g, '>');
115   - str = str.replace(/&amp;/g, '&');
116   - return str;
117   - }
118   - /**
119   - * @description parse XML string
120   - * @param xml{String} XML string
121   - * @param handler{Object} callback handler
122   - * @return void
123   - */
124   - _parseXML = function(xml, handler ) {
125   - var index, chars, match, stack = [], last = xml;
126   - stack.last = function(){
127   - return this[ this.length - 1 ];
128   - };
129   -
130   - while (xml) {
131   - chars = true;
132   -
133   - //CDATA
134   - if(xml.indexOf("<![CDATA[") == 0){
135   -
136   - index = xml.indexOf("]]>");
137   - if(index > 0){
138   - text = xml.substring(9, index);
139   -
140   - if ( handler.chars )
141   - handler.chars( text, true );
142   -
143   - xml = xml.substring( index + 3 );
144   - }
145   - chars = false;
146   - }
147   - //instruction
148   - else if(xml.indexOf("<?") == 0){
149   - index = xml.indexOf("?>");
150   - if(index > 0){
151   - text = xml.substr(2, index-2);
152   - if(handler.instruction){
153   - handler.instruction(text);
154   - }
155   - xml = xml.substring( index + 2 );
156   - }
157   - chars = false;
158   - }
159   - // Comment
160   - else if ( xml.indexOf("<!--") == 0 ) {
161   - index = xml.indexOf("-->");
162   -
163   - if ( index >= 0 ) {
164   - if ( handler.comment )
165   - handler.comment( xml.substring( 4, index ) );
166   - xml = xml.substring( index + 3 );
167   - chars = false;
168   - }
169   -
170   - // end tag
171   - } else if ( xml.indexOf("</") == 0 ) {
172   - match = xml.match( endTag );
173   -
174   - if ( match ) {
175   - xml = xml.substring( match[0].length );
176   - match[0].replace( endTag, parseEndTag );
177   - chars = false;
178   - }
179   -
180   - // start tag
181   - } else if ( xml.indexOf("<") == 0 ) {
182   - match = xml.match( startTag );
183   - if ( match ) {
184   - xml = xml.substring( match[0].length );
185   - match[0].replace( startTag, parseStartTag );
186   - chars = false;
187   - }
188   - }
189   -
190   - if ( chars ) {
191   - index = xml.indexOf("<");
192   -
193   - var text = index < 0 ? xml : xml.substring( 0, index );
194   - xml = index < 0 ? "" : xml.substring( index );
195   -
196   - if ( handler.chars )
197   - handler.chars( text );
198   - }
199   -
200   -
201   - if ( xml == last )
202   - throw "Parse Error: " + xml;
203   - last = xml;
204   - }
205   -
206   - // Clean up any remaining tags
207   - parseEndTag();
208   -
209   - function parseStartTag(tag, tagName, rest, unary ) {
210   -
211   - unary = !!unary;
212   -
213   - if ( !unary )
214   - stack.push( tagName );
215   -
216   - if ( handler.start ) {
217   - var attrs = [];
218   -
219   - rest.replace(attr, function(match, name) {
220   - var value = arguments[2] ? arguments[2] :
221   - arguments[3] ? arguments[3] :
222   - arguments[4] ? arguments[4] :
223   - name ? name : "";
224   -
225   - attrs.push({
226   - name: name,
227   - value: value,
228   - escaped: value.replace(/(^|[^\\])"/g, '$1\\\"') //"
229   - });
230   - });
231   -
232   - if ( handler.start )
233   - handler.start( tagName, attrs, unary );
234   - }
235   - }
236   -
237   - function parseEndTag( tag, tagName ) {
238   - // If no tag name is provided, clean shop
239   - if ( !tagName )
240   - var pos = 0;
241   -
242   - // Find the closest opened tag of the same type
243   - else
244   - for ( var pos = stack.length - 1; pos >= 0; pos-- )
245   - if ( stack[ pos ] == tagName )
246   - break;
247   -
248   - if ( pos >= 0 ) {
249   - // Close all the open elements, up the stack
250   - for ( var i = stack.length - 1; i >= pos; i-- )
251   - if ( handler.end )
252   - handler.end( stack[ i ] );
253   -
254   - // Remove the open elements from the stack
255   - stack.length = pos;
256   - }
257   - }
258   - };
259   -
260   - /**
261   - * @description Namespace class
262   - * @access public
263   - * @param prefix{String} the namespace prefix
264   - * @param uri{String} the namespace uri.
265   - */
266   - Namespace = function(prefix, uri){
267   - var len = arguments.length;
268   - if(len >= 2){
269   - this.prefix = String(prefix);
270   - this.uri = String(uri);
271   - }else if(len == 1){
272   - this.prefix = "";
273   - this.uri = String(prefix);
274   - }else{
275   - this.prefix = "";
276   - this.uri = "";
277   - }
278   - }
279   -
280   - Namespace.prototype = {
281   - constructor: Namespace,
282   -
283   - /**
284   - * @description to string
285   - * @access public
286   - * @return String
287   - */
288   - toString: function(){
289   - return this.uri;
290   - },
291   - /**
292   - * @description return a Namespace copy object
293   - * @access public
294   - * @return Namespace
295   - */
296   - copy: function(){
297   - var ns = new Namespace();
298   - ns.prefix = this.prefix;
299   - ns.uri = this.uri;
300   - return ns;
301   - },
302   - /**
303   - * @description check if the two Namespace are equivalent
304   - * @access public
305   - * @return Boolean
306   - */
307   - equals: function(ns){
308   - return this.prefix === ns.prefix && this.uri === ns.uri;
309   - }
310   - }
311   - /**
312   - * @description QName class
313   - * @access public
314   - * @param uri {Namespace | String}
315   - * @param localName {String}
316   - */
317   - QName = function(uri, localName){
318   - var len = arguments.length;
319   - if(len >= 2){
320   - this.uri = String(uri);
321   - this._ns = (uri && uri.constructor == Namespace) ? uri : new Namespace(uri);
322   - this.localName = String(localName);
323   - }else if(len == 1){
324   - this.uri = "";
325   - this._ns = new Namespace();
326   - this.localName = String(uri);
327   - }else{
328   - this.uri = "";
329   - this._ns = new Namespace();
330   - this.localName = "";
331   - }
332   - }
333   - QName.prototype = {
334   - constructor: QName,
335   -
336   - /**
337   - * @description to string
338   - * @return String
339   - */
340   - toString: function(){
341   - var r = this.uri ? this.uri + "::" : "";
342   - return r +this.localName;
343   - },
344   - /**
345   - * @description return a QName copy object
346   - * @return QName
347   - */
348   - copy: function(){
349   - var qn = new QName();
350   - qn.uri = this.uri;
351   - qn.localName = this.localName;
352   - qn._ns = this._ns.copy();
353   - return qn;
354   - },
355   - /**
356   - * @description check if the two QName are equivalent
357   - * @access public
358   - * @return Boolean
359   - */
360   - equals: function(qname){
361   - return this.localName == qname.localName && this._ns.equals(qname._ns);
362   - }
363   - }
364   - /**
365   - * @description parse a name & value to a QName
366   - * @access internal
367   - * @static
368   - * @param name{String} namespace declaration name
369   - * @param value{String} namespace declaration value
370   - * @return QName
371   - */
372   - QName._format = function(name, value){
373   - var temp = name.split(":"), prefix, localName;
374   - if(temp.length==2){
375   - prefix = temp[0];
376   - localName = temp[1];
377   - }else{
378   - prefix = '';
379   - localName = name;
380   - }
381   - return new QName(new Namespace(prefix, value), localName);
382   - }
383   - /**
384   - * @description NodeKind class
385   - * @static
386   - */
387   - NodeKind = {
388   - 'ELEMENT': 'element',
389   - 'COMMENT': 'comment',
390   - 'PROCESSING_INSTRUCTIONS': 'processing-instructions',
391   - 'TEXT': 'text',
392   - 'ATTRIBUTE': 'attribute'
393   - }
394   -
395   - /**
396   - * @description get a suitable class for XML operations by the XMLList.
397   - * if the list contains only one child, then return the child XML.
398   - * @return XML|XMLList
399   - */
400   - _getXMLIfLengthEqualOne = function(list){
401   - if(list._list && list.length() == 1){
402   - return _getXMLIfLengthEqualOne(list._list[0]);
403   - }
404   - return list;
405   - };
406   - /**
407   - * @description XMLList is a XML collection
408   - * @param null
409   - */
410   - XMLList= function(){
411   - this._list = [];
412   - }
413   - merge(XMLList.prototype, {
414   - constructor: XMLList,
415   -
416   - /**
417   - * @description add a xml to the list
418   - * @access private
419   - * @return XMLList
420   - */
421   - _addXML: function(xml){
422   - if(xml.constructor == XML){
423   - this._list.push(xml);
424   - }else if(xml.constructor == XMLList){
425   - this._list = this._list.concat(xml._list);
426   - }
427   - return this;
428   - },
429   - /**
430   - * @description get the list length
431   - * @access public
432   - * @return int
433   - */
434   - length: function(){
435   - return this._list.length;
436   - },
437   - /**
438   - * @description you cannot call this method in a list, only XML owns childIndex method.
439   - * @access none
440   - * @return void
441   - */
442   - childIndex: function(){
443   - throw new Error("this method only availabe in single list XML");
444   - },
445   - /**
446   - * @description return all the child by localName in this xml list.
447   - * @access public
448   - * @return XMLList | XML
449   - */
450   - child: function(p){
451   - var list = new XMLList();
452   - this.each(function(item){
453   - var child = item.child(p);
454   - if(child.length()){
455   - list._addXML(child);
456   - }
457   - });
458   - return _getXMLIfLengthEqualOne(list);
459   - },
460   - /**
461   - * @description return all the child in this xml list.
462   - * @access public
463   - * @return XMLList | XML
464   - */
465   - children: function(){
466   - return this.child('*');
467   - },
468   - /**
469   - * @description return a special attribute type child by parameter
470   - * @access public
471   - * @param p{String} if the p is '*', return all attributes
472   - * @return XML | XMLList
473   - * @see attributes
474   - */
475   - attribute: function(p){
476   - var list = new XMLList();
477   - this.each(function(item){
478   - list._addXML(item.attribute(p));
479   - });
480   - return _getXMLIfLengthEqualOne(list);
481   - },
482   - /**
483   - * @description return all attribute type child.
484   - * @access public
485   - * @return XML | XMLList
486   - * @see attribute
487   - */
488   - attributes: function(p){
489   - return this.attribute('*');
490   - },
491   - /**
492   - * @description find special elements child from the XMLList tree top.
493   - * @access public
494   - * @param p{String} the element localName, if the localName is '*', return all the element child.
495   - * @return XML | XMLList
496   - */
497   - elements: function(p){
498   - var list = new XMLList();
499   - this.each(function(item){
500   - list._addXML(item.elements(p));
501   - });
502   - return _getXMLIfLengthEqualOne(list);
503   - },
504   - /**
505   - * @description find descendants child from the XMLList tree top.
506   - * @access public
507   - * @param p{String} the descendant localName, if the localName is '*', return all the descendants.
508   - * @return XML | XMLList
509   - */
510   - descendants: function(p){
511   - var list = new XMLList();
512   - this.each(function(item){
513   - list._addXML(item.descendants(p));
514   - });
515   - return _getXMLIfLengthEqualOne(list);
516   - },
517   - /**
518   - * @description merge multi text childs nearby to one text child of each list item.
519   - * @access public
520   - * @return void
521   - */
522   - normalize: function(){
523   - this.each(function(item){
524   - item.normalize();
525   - });
526   - },
527   - /**
528   - * @description check if all the list item owns simple content only
529   - * @access public
530   - * @return Boolean
531   - * @see hasComplexContent
532   - */
533   - hasSimpleContent: function(){
534   - for(var i=0, l = this._list.length; i < l; i++){
535   - var item = this._list[i];
536   - if(item.constructor == XMLList ||
537   - item.hasComplexContent()){
538   - return false;
539   - }
540   - }
541   - return true;
542   - },
543   - /**
544   - * @description check if all the list item owns simple content only
545   - * @access public
546   - * @return Boolean
547   - * @see hasSimpleContent
548   - */
549   - hasComplexContent: function(){
550   - return !this.hasSimpleContent();
551   - },
552   - /**
553   - * @description return the text type child
554   - * @access public
555   - * @return XML
556   - */
557   - text: function(){
558   - var xml = new XMLList();
559   - this.each(function(item){
560   - if(item.constructor == XML){
561   - var t = item.text();
562   - if(t._text != ""){
563   - xml._addXML(t);
564   - }
565   - }
566   - });
567   - return _getXMLIfLengthEqualOne(xml);
568   - },
569   - /**
570   - * @description return the comment type child
571   - * @access public
572   - * @return XML | XMLList
573   - */
574   - comments: function(){
575   - var xml = new XMLList();
576   - this.each(function(item){
577   - if(item.constructor == XML){
578   - xml._addXML(item.comments());
579   - }
580   - });
581   - return _getXMLIfLengthEqualOne(xml);
582   - },
583   - /**
584   - * @description return the XML processing-instructions.
585   - * @access public
586   - * @return XMLList | XML
587   - */
588   - processingInstructions: function(){
589   - var xml = new XMLList();
590   - this.each(function(item){
591   - if(item.constructor == XML){
592   - xml._addXML(item.processingInstructions());
593   - }
594   - });
595   - return _getXMLIfLengthEqualOne(xml);
596   - },
597   - /**
598   - * @description return an XMLList copy object.
599   - * @access public
600   - * @return XMLList
601   - */
602   - copy: function(){
603   - var list = new XMLList();
604   - this.each(function(item, i){
605   - list._list[i] = item.copy();
606   - });
607   - return list;
608   - },
609   - /**
610   - * @description return toXMLString of all the list item
611   - * @access public
612   - * @return String
613   - */
614   - toXMLString: function(){
615   - var s = [];
616   - this.each(function(item){
617   - s.push(item.toXMLString());
618   - });
619   - return s.join("\n");
620   - },
621   - /**
622   - * @description return toString of all the list item.
623   - * @access public
624   - * @return String
625   - */
626   - toString: function(){
627   - var s = "";
628   - this.each(function(item){
629   - s += item.toString();
630   - });
631   - return s;
632   - },
633   - //===================extension for javascript=================
634   - /**
635   - * @description return a special item by item index.
636   - * @access public
637   - * @param n{int} the item index
638   - * @return XML
639   - */
640   - item: function(n){
641   - return this._list[n];
642   - },
643   - /**
644   - * @description for loop the list item.
645   - * @access public
646   - * @param func{Function} the callback handler
647   - * @return void
648   - */
649   - each: function(func){
650   - for(var i = 0, l = this._list.length; i <l; i++){
651   - func(this._list[i], i, this);
652   - }
653   - }
654   - });
655   - /**
656   - * @description XML class
657   - * @param str{String}
658   - */
659   - XML = function(str){
660   - this._children = [];
661   - this._attributes = [];
662   - this._namespaces = [];
663   - this._nodeKind = NodeKind.ELEMENT;
664   - this._qname = null;
665   - this._parent = null;
666   - this._text = null;
667   - this._useCDATA = false;
668   -
669   - var current, self= this;
670   - if(str){
671   - _parseXML(str, {
672   - start: function(tag, attrs, unary ){
673   - var xml;
674   - if(!current){
675   - xml = self;
676   - }else{
677   - xml = new XML();
678   - xml._parent = current;
679   - }
680   - xml._qname = QName._format(tag);
681   - for(var i in attrs){
682   - var attr = new XML();
683   - attr._nodeKind = NodeKind.ATTRIBUTE;
684   - var _qname;
685   - if(attrs[i].name === 'xmlns'){
686   - _qname = new QName(new Namespace('xmlns', attrs[i].value), '');
687   - }else{
688   - _qname = QName._format(attrs[i].name, attrs[i].value);
689   - }
690   - var prefix = _qname._ns.prefix || "";
691   -
692   - if(prefix === 'xmlns'){
693   - var ns = new Namespace(_qname.localName, _qname.uri);
694   - xml.addNamespace(ns);
695   - if(_qname.localName == xml._qname._ns.prefix){
696   - xml.setNamespace(ns);
697   - }
698   - }else{
699   - attr._qname = _qname;
700   - attr._text = attrs[i].value;
701   - xml._attributes.push(attr);
702   - }
703   - }
704   - current = xml;
705   - if(unary){
706   - this.end(tag);
707   - }
708   - },
709   - chars: function(text, useCDATA){
710   - text = trim(text);
711   - if(text == "" && XML.ignoreWhitespace){
712   - return;
713   - }
714   - var el = new XML();
715   - el._nodeKind = NodeKind.TEXT;
716   - el._text = text;
717   - el._useCDATA = useCDATA;
718   - current._children.push(el);
719   - },
720   - end: function(tag){
721   - if(current && current._parent){
722   - current._parent._children.push(current);
723   - current = current._parent;
724   - }else if(current == self){
725   - current = null;
726   - }
727   - },
728   - comment: function(value){
729   - var el = new XML();
730   - el._nodeKind = NodeKind.COMMENT;
731   - el._text = value;
732   - current && current._children.push(el);
733   - },
734   - instruction: function(value){
735   - var el = new XML();
736   - el._nodeKind = NodeKind.PROCESSING_INSTRUCTIONS;
737   - el._text = value;
738   - current && current._children.push(el);
739   - }
740   - });
741   - }
742   - }
743   -
744   - merge(XML.prototype, {
745   - /**
746   - * @description add new namespace to this XML
747   - * @access public
748   - * @param ns{Namespace}
749   - * @return void
750   - */
751   - addNamespace: function(ns){
752   - if(ns.prefix !=undefined){
753   - this.removeNamespace(ns);
754   - this._namespaces.push(ns);
755   - }
756   - },
757   - /**
758   - * @description remove a namespace from this XML
759   - * @access public
760   - * @param ns{Namespace}
761   - * @return void
762   - */
763   - removeNamespace: function(ns){
764   - for(var i = 0, l = this._namespaces.length; i < l; i++){
765   - if(ns.prefix==this._namespaces[i].prefix){
766   - this._namespaces.splice(i, 1);
767   - break;
768   - }
769   - }
770   - },
771   - /**
772   - * @description reture a namespace by prefix
773   - * @access public
774   - * @param prefix{String}
775   - * @return Namespace
776   - */
777   - namespace: function(prefix){
778   - if(!prefix){
779   - return new Namespace();
780   - }
781   - for(var i = 0, l = this._namespaces.length; i < l; i++){
782   - if(prefix==this._namespaces[i].prefix){
783   - return this._namespaces[i];
784   - }
785   - }
786   - return undefined;
787   - },
788   - /**
789   - * @description set the namespace for this XML
790   - * @access public
791   - * @param ns{Namespace}
792   - * @return void
793   - */
794   - setNamespace: function(ns){
795   - if(ns && ns.constructor == Namespace){
796   - this.addNamespace(ns);
797   - if(this._qname){
798   - this._qname.uri = ns.uri;
799   - this._qname._ns = ns;
800   - }
801   - }
802   - },
803   - /**
804   - * @description return declarated namespace of this XML
805   - * @access public
806   - * @return Array
807   - */
808   - namespaceDeclarations: function(){
809   - return this._namespaces;
810   - },
811   - /**
812   - * @description return declarated namespace of this XML and all parent XML.
813   - * @access public
814   - * @return Array
815   - */
816   - inScopeNamespaces: function(){
817   - var array = this._namespaces;
818   - var chain = this._parent;
819   - while(chain){
820   - array = chain.inScopeNamespaces().concat(array);
821   - chain = chain._parent;
822   - }
823   - return array;
824   - },
825   - /**
826   - * @description return the nodekind of this element
827   - * @access public
828   - * @return String
829   - * @see NodeKind
830   - */
831   - nodeKind: function(){
832   - return this._nodeKind;
833   - },
834   - /**
835   - * @description return the full name (with declarated namespace) of this xml
836   - * @access public
837   - * @return String
838   - * @see localName
839   - */
840   - name: function(){
841   - if(!this._qname){
842   - return null;
843   - }
844   - if(this._qname.uri){
845   - return this._qname.uri +":"+this._qname.localName;
846   - }
847   - return this._qname.localName;
848   - },
849   - /**
850   - * @description return the local name (without declarated namespace) of this xml
851   - * @access public
852   - * @return String
853   - * @see name
854   - */
855   - localName: function(){
856   - if(!this._qname){
857   - return null;
858   - }
859   - return this._qname.localName;
860   - },
861   - /**
862   - * @description set the full name (with declarated namespace) of this xml
863   - * @access public
864   - * @return void
865   - * @see name
866   - */
867   - setName: function(name){
868   - if(this._qname == null){
869   - return;
870   - }
871   - if(/^[a-zA-Z]+[a-zA-Z0-9]*$/.test(name)){
872   - this._qname.uri = "";
873   - this._qname.localName = name;
874   - }else{
875   - throw new Error("invalid value for XML name");
876   - }
877   - },
878   - /**
879   - * @description set the local name (without declarated namespace) of this xml
880   - * @access public
881   - * @return void
882   - * @see localName
883   - */
884   - setLocalName: function(name){
885   - if(this._qname == null){
886   - return;
887   - }
888   - if(/^[a-zA-Z\$_]+[a-zA-Z0-9\$\-_]*$/.test(name)){
889   - this._qname.localName = name;
890   - }else{
891   - throw new Error("invalid value for XML localName");
892   - }
893   - },
894   - /**
895   - * @description get the length of this xml tree, return 1 always for XML.
896   - * @access public
897   - * @return int
898   - */
899   - length: function(){
900   - return 1;
901   - },
902   - /**
903   - * @description return a special attribute type child by param
904   - * @access public
905   - * @param p{String} if the p is '*', return all attributes
906   - * @return XML | XMLList
907   - * @see attributes
908   - */
909   - attribute: function(p){
910   - var attributes = this._attributes,
911   - i,
912   - l,
913   - item,
914   - list = new XMLList();
915   - for( i =0, l = attributes.length; i <l; i++){
916   - item = attributes[i];
917   - if(item._qname.localName == p || p == '*'){
918   - list._addXML(item);
919   - }
920   - }
921   - return _getXMLIfLengthEqualOne(list);
922   - },
923   - /**
924   - * @description return all attribute type child.
925   - * @access public
926   - * @return XML | XMLList
927   - * @see attribute
928   - */
929   - attributes: function(){
930   - return this.attribute('*');
931   - },
932   - /**
933   - * @description create a text type XML node by the text parameter,
934   - * if the text contains special characters, use CDATA tag.
935   - * @access private
936   - * @param text{String}
937   - * @return XML
938   - */
939   - _createTextNode: function(text){
940   - var el = new XML();
941   - el._nodeKind = NodeKind.TEXT;
942   - el._text = text;
943   - el._useCDATA =/['"<>&]/.test(text);
944   - return el;
945   - },
946   - /**
947   - * @description append child to the children list
948   - * @access public
949   - * @param child{XML | XMLList | String} the child need to be add
950   - * @return XML
951   - * @see prependChild
952   - */
953   - appendChild: function(child){
954   - var cc = child.constructor;
955   - if(cc == XML){
956   - child._parent = this;
957   - this._children.push(child);
958   - }else if(cc == XMLList){
959   - child.each(function(item){
960   - item._parent = this;
961   - });
962   - this._children = this._children.concat(child._list);
963   - }else if(cc == String){
964   - var c = this._createTextNode(child);
965   - c._parent = this;
966   - this._children.push(c);
967   - }
968   - return this;
969   - },
970   - /**
971   - * @description prepend child to the children list
972   - * @access public
973   - * @param child{XML | XMLList | String} the child need to be add
974   - * @return XML
975   - * @see appendChild
976   - */
977   - prependChild: function(child){
978   - var cc = child.constructor;
979   - if(cc == XML){
980   - child._parent = this;
981   - this._children.unshift(child);
982   - }else if(cc == XMLList){
983   - child.each(function(item){
984   - item._parent = this;
985   - });
986   - this._children = this._list.concat(this._children);
987   - }else if(cc === String){
988   - var c = this._createTextNode(child);
989   - c._parent = this;
990   - this._children.unshift(c);
991   - }
992   - return this;
993   - },
994   - /**
995   - * @description merge multi text childs nearby to one text child.
996   - * @access public
997   - * @return void
998   - */
999   - normalize: function(){
1000   - var i,
1001   - l,
1002   - preTextEl,
1003   - _c = this._children,
1004   - newChildren = [];
1005   -
1006   - for(i = 0, l = _c.length; i < l; i++){
1007   - var item = _c[i],
1008   - nk = item.nodeKind();
1009   - if(nk == NodeKind.TEXT){
1010   - if(preTextEl){
1011   - item._text = preTextEl._text + item._text;
1012   - _c[i-1] = null;
1013   - }
1014   - preTextEl = item;
1015   - }else if(nk == NodeKind.ELEMENT){
1016   - item.normalize();
1017   - preTextEl = null;
1018   - }
1019   - }
1020   - for(i = 0, l = _c.length; i < l; i++){
1021   - _c[i] && newChildren.push(_c[i]);
1022   - }
1023   - this._children = newChildren;
1024   - },
1025   - /**
1026   - * @description return a filter children list, if the ignoreComments is true, the list will
1027   - * not contains any comment child, same as processing-instructions child.
1028   - * @access private
1029   - * @return Array
1030   - */
1031   - _getFilterChildren: function(){
1032   - var i,
1033   - l,
1034   - c = [],
1035   - _c = this._children;
1036   -
1037   - for(i = 0, l = _c.length; i < l; i++){
1038   - var item = _c[i],
1039   - nk = item.nodeKind();
1040   - if(nk == NodeKind.ELEMENT || nk == NodeKind.TEXT ||
1041   - (nk == NodeKind.COMMENT && !XML.ignoreComments) ||
1042   - (nk == NodeKind.PROCESSING_INSTRUCTIONS && !XML.ignoreProcessingInstructions)){
1043   - c.push(item);
1044   - }
1045   - }
1046   - return c;
1047   - },
1048   - /**
1049   - * @description find a child by localName
1050   - * @access public
1051   - * @param p{String} the child localName, if the localName is '*', return all the child.
1052   - * @return XML | XMLList
1053   - */
1054   - child: function(p){
1055   - var list = new XMLList(),