文件: dojo/dir1/utils/XmlJsonUtils.js
// Author: Rajat Khandelwal
define([
"dojo/_base/declare" // declare
], function(declare){
return declare("dir1.utils.XmlJsonUtils",[],{
parseXml : function (xml) {
var self=this;
var dom = null;
if (window.DOMParser) {
try {
dom = (new DOMParser()).parseFromString(xml, "text/xml");
}
catch (e) { dom = null; }
}
else if (window.ActiveXObject) {
try {
dom = new ActiveXObject('Microsoft.XMLDOM');
dom.async = false;
if (!dom.loadXML(xml)) // parse error ..
window.alert(dom.parseError.reason + dom.parseError.srcText);
}
catch (e) { dom = null; }
}
else
alert("cannot parse xml string!");
return dom;
},
xml2json : function (xmldata)
{
var self=this;
if(xmldata.firstChild==null)
{
return {name:xmldata.nodeName+": (value null)", checked: true}
}
else if(xmldata.firstChild.nodeType==3)
{
return {name:xmldata.nodeName+": "+xmldata.firstChild.nodeValue, checked:true}
}
else
{
var mychildren=[];
var i=0;
var nochildren=xmldata.childElementCount
for(i=0;i<nochildren;i++)
{
var j=self.xml2json(xmldata.childNodes[i])
mychildren[i]=j
}
var ret= {name:xmldata.nodeName, children:mychildren, checked:true}
return ret
}
},
convert2arr : function (result,ctr,res_arr)
{
var self=this;
if(result[ctr].checked[0]==false)
return;
if(result[ctr].children==undefined)
{
var name=result[ctr]['name'][0];
var kv = name.split(': ');
if(kv[1]=="(value null)")
kv[1]="";
res_arr.push.apply(res_arr,["<",kv[0],">",kv[1],"</",kv[0],">"]);
return ctr+1;
}
else
{
var i=ctr;
var new_ctr=ctr;
var no_children=result[ctr].children.length;
res_arr.push.apply(res_arr,["<",result[ctr].name[0],">"])
for(i=0;i<no_children;i++)
{
new_ctr=self.convert2arr(result,result[ctr].children[i]._0,res_arr)
}
res_arr.push.apply(res_arr,["</",result[ctr].name[0],">"]);
return new_ctr;
}
},
convert2xml : function (result)
{
var arr=[]
self.convert2arr(result, 0, arr)
return arr.join('')
}
})
})
但是当我在代码require
中时dir1.utils.XmlJsonUtils
,它会说Uncaught Error: declare XmlJsonUtils: base class is not a callable constructor
。声明某些实用程序函数的正确方法是什么。
那些应该像静态函数。我不想做x=new XmlJsonUtils(); x.parseXml(..)
。我想要做XmlJsonUtils.parseXml(..)