dojox.xml.DomParser.parse
dojo.require("dojox.xml.DomParser");
defined in dojox/xml/DomParser.js
Usage
function (/*String*/ str) (view source)
var root=_doc(); if(str==null){ return root; } if(str.length==0){ return root; } // preprocess custom entities if(str.indexOf("<!ENTITY")>0){ var entity, eRe=[]; if(reEntity.test(str)){ reEntity.lastIndex=0; // match entities while((entity=reEntity.exec(str))!=null){ eRe.push({ entity:"&"+entity[1].replace(trim,"")+";", expression:entity[2] }); } // replace instances in the document. for(var i=0; i<eRe.length; i++){ str=str.replace(new RegExp(eRe[i].entity, "g"), eRe[i].expression); } } } // pre-parse for CData, and tokenize. var cdSections=[], cdata; while((cdata=reCData.exec(str))!=null){ cdSections.push(cdata[1]); } for(var i=0; i<cdSections.length; i++){ str=str.replace(cdSections[i], i); } // pre-parse for comments, and tokenize. var comments=[], comment; while((comment=reComments.exec(str))!=null){ comments.push(comment[1]); } for(i=0; i<comments.length; i++){ str=str.replace(comments[i], i); } // parse the document var res, obj=root; while((res=reTags.exec(str))!=null){ // closing tags. if(res[2].charAt(0)=="/"){ if(obj.parentNode){ obj=obj.parentNode; } var text=res[3]; if(text.length>0) obj.appendChild(_createTextNode(text)); }else // open tags. if(res[1].length>0){ // figure out the type of node. if(res[1].charAt(0)=="?"){ // processing instruction var name=res[1].substr(1); var target=res[2].substr(0,res[2].length-2); obj.childNodes.push({ nodeType:nodeTypes.PROCESSING_INSTRUCTION, nodeName:name, nodeValue:target }); } else if(res[1].charAt(0)=="!"){ // CDATA; skip over any declaration elements. if(res[1].indexOf("![CDATA[")==0){ var val=parseInt(res[1].replace("![CDATA[","").replace("]]","")); obj.childNodes.push({ nodeType:nodeTypes.CDATA_SECTION, nodeName:"#cdata-section", nodeValue:cdSections[val] }); } // Comments. else if(res[1].substr(0,3)=="!--"){ var val=parseInt(res[1].replace("!--","").replace("--","")); obj.childNodes.push({ nodeType:nodeTypes.COMMENT, nodeName:"#comment", nodeValue:comments[val] }); } } else { // Elements (with attribute and text) var name=res[1].replace(trim,""); var o={ nodeType:nodeTypes.ELEMENT, nodeName:name, localName:name, namespace:dNs, ownerDocument:root, attributes:[], parentNode:null, childNodes:[] }; // check to see if it's namespaced. if(name.indexOf(":")>-1){ var t=name.split(":"); o.namespace=t[0]; o.localName=t[1]; } // set the function references. o.byName=o.getElementsByTagName=byName; o.byNameNS=o.getElementsByTagNameNS=byNameNS; o.childrenByName=childrenByName; o.getAttribute=getAttr; o.getAttributeNS=getAttrNS; o.setAttribute=setAttr; o.setAttributeNS=setAttrNS; o.previous=o.previousSibling=prev; o.next=o.nextSibling=next; // parse the attribute string. var attr; while((attr=reAttr.exec(res[2]))!=null){ if(attr.length>0){ var name=attr[1].replace(trim,""); var val=attr[2].replace(normalize," ") .replace(egt,">") .replace(elt,"<") .replace(eapos,"'") .replace(equot,'"') .replace(eamp,"&"); if(name.indexOf("xmlns")==0){ if(name.indexOf(":")>0){ var ns=name.split(":"); root.namespaces[ns[1]]=val; root._nsPaths[val]=ns[1]; } else { root.namespaces[dNs]=val; root._nsPaths[val]=dNs; } } else { var ln=name; var ns=dNs; if(name.indexOf(":")>0){ var t=name.split(":"); ln=t[1]; ns=t[0]; } o.attributes.push({ nodeType:nodeTypes.ATTRIBUTE, nodeName:name, localName:ln, namespace:ns, nodeValue:val }); // only add id as a property. if(ln=="id"){ o.id=val; } } } } root._add(o); if(obj){ obj.childNodes.push(o); o.parentNode=obj; // if it's not a self-closing node. if(res[2].charAt(res[2].length-1)!="/"){ obj=o; } } var text=res[3]; if(text.length>0){ obj.childNodes.push(_createTextNode(text)); } } } } // set the document element for(var i=0; i<root.childNodes.length; i++){ var e=root.childNodes[i]; if(e.nodeType==nodeTypes.ELEMENT){ root.documentElement=e; break; } } return root;
parameter | type | description |
---|---|---|
str | String |