root / trunk / web / dojo / dojox / xml / parser.js @ 13
History | View | Annotate | Download (2.81 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.xml.parser"]){ |
||
9 | dojo._hasResource["dojox.xml.parser"]=true; |
||
10 | dojo.provide("dojox.xml.parser");
|
||
11 | dojox.xml.parser.parse=function(_1,_2){ |
||
12 | var _3=dojo.doc;
|
||
13 | var _4;
|
||
14 | _2=_2||"text/xml";
|
||
15 | if(_1&&dojo.trim(_1)&&"DOMParser" in dojo.global){ |
||
16 | var _5=new DOMParser(); |
||
17 | _4=_5.parseFromString(_1,_2); |
||
18 | var de=_4.documentElement;
|
||
19 | var _6="http://www.mozilla.org/newlayout/xml/parsererror.xml"; |
||
20 | if(de.nodeName=="parsererror"&&de.namespaceURI==_6){ |
||
21 | var _7=de.getElementsByTagNameNS(_6,"sourcetext")[0]; |
||
22 | if(_7){
|
||
23 | _7=_7.firstChild.data; |
||
24 | } |
||
25 | throw new Error("Error parsing text "+de.firstChild.data+" \n"+_7); |
||
26 | } |
||
27 | return _4;
|
||
28 | }else{
|
||
29 | if("ActiveXObject" in dojo.global){ |
||
30 | var ms=function(n){ |
||
31 | return "MSXML"+n+".DOMDocument"; |
||
32 | }; |
||
33 | var dp=["Microsoft.XMLDOM",ms(6),ms(4),ms(3),ms(2)]; |
||
34 | dojo.some(dp,function(p){
|
||
35 | try{
|
||
36 | _4=new ActiveXObject(p);
|
||
37 | } |
||
38 | catch(e){
|
||
39 | return false; |
||
40 | } |
||
41 | return true; |
||
42 | }); |
||
43 | if(_1&&_4){
|
||
44 | _4.async=false;
|
||
45 | _4.loadXML(_1); |
||
46 | var pe=_4.parseError;
|
||
47 | if(pe.errorCode!==0){ |
||
48 | throw new Error("Line: "+pe.line+"\n"+"Col: "+pe.linepos+"\n"+"Reason: "+pe.reason+"\n"+"Error Code: "+pe.errorCode+"\n"+"Source: "+pe.srcText); |
||
49 | } |
||
50 | } |
||
51 | if(_4){
|
||
52 | return _4;
|
||
53 | } |
||
54 | }else{
|
||
55 | if(_3.implementation&&_3.implementation.createDocument){
|
||
56 | if(_1&&dojo.trim(_1)&&_3.createElement){
|
||
57 | var _8=_3.createElement("xml"); |
||
58 | _8.innerHTML=_1; |
||
59 | var _9=_3.implementation.createDocument("foo","",null); |
||
60 | dojo.forEach(_8.childNodes,function(_a){
|
||
61 | _9.importNode(_a,true);
|
||
62 | }); |
||
63 | return _9;
|
||
64 | }else{
|
||
65 | return _3.implementation.createDocument("","",null); |
||
66 | } |
||
67 | } |
||
68 | } |
||
69 | } |
||
70 | return null; |
||
71 | }; |
||
72 | dojox.xml.parser.textContent=function(_b,_c){ |
||
73 | if(arguments.length>1){ |
||
74 | var _d=_b.ownerDocument||dojo.doc;
|
||
75 | dojox.xml.parser.replaceChildren(_b,_d.createTextNode(_c)); |
||
76 | return _c;
|
||
77 | }else{
|
||
78 | if(_b.textContent!==undefined){ |
||
79 | return _b.textContent;
|
||
80 | } |
||
81 | var _e=""; |
||
82 | if(_b){
|
||
83 | dojo.forEach(_b.childNodes,function(_f){
|
||
84 | switch(_f.nodeType){
|
||
85 | case 1: |
||
86 | case 5: |
||
87 | _e+=dojox.xml.parser.textContent(_f); |
||
88 | break;
|
||
89 | case 3: |
||
90 | case 2: |
||
91 | case 4: |
||
92 | _e+=_f.nodeValue; |
||
93 | } |
||
94 | }); |
||
95 | } |
||
96 | return _e;
|
||
97 | } |
||
98 | }; |
||
99 | dojox.xml.parser.replaceChildren=function(_10,_11){ |
||
100 | var _12=[];
|
||
101 | if(dojo.isIE){
|
||
102 | dojo.forEach(_10.childNodes,function(_13){
|
||
103 | _12.push(_13); |
||
104 | }); |
||
105 | } |
||
106 | dojox.xml.parser.removeChildren(_10); |
||
107 | dojo.forEach(_12,dojo.destroy); |
||
108 | if(!dojo.isArray(_11)){
|
||
109 | _10.appendChild(_11); |
||
110 | }else{
|
||
111 | dojo.forEach(_11,function(_14){
|
||
112 | _10.appendChild(_14); |
||
113 | }); |
||
114 | } |
||
115 | }; |
||
116 | dojox.xml.parser.removeChildren=function(_15){ |
||
117 | var _16=_15.childNodes.length;
|
||
118 | while(_15.hasChildNodes()){
|
||
119 | _15.removeChild(_15.firstChild); |
||
120 | } |
||
121 | return _16;
|
||
122 | }; |
||
123 | dojox.xml.parser.innerXML=function(_17){ |
||
124 | if(_17.innerXML){
|
||
125 | return _17.innerXML;
|
||
126 | }else{
|
||
127 | if(_17.xml){
|
||
128 | return _17.xml;
|
||
129 | }else{
|
||
130 | if(typeof XMLSerializer!="undefined"){ |
||
131 | return (new XMLSerializer()).serializeToString(_17); |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | return null; |
||
136 | }; |
||
137 | } |