一个类似与PHP的var_dump函数的方法(打印一个变量的结构-包括复杂的array和object)。
if(typeof(Class)=='undefined'){
var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}
}
var StrR=function(str,len){
var Str="";
for(var i=0;i<len;i++)
Str+=str;
return Str;
}
/*
* It is a function has similar function of PHP function var_dump
* @usage:
var v=new VAR(xml);// xml is the variable which you want to print out.
msg=v.dump(); // msg contains the construct of the variable.
*/
var VAR=Class.create();
VAR.prototype = {
initialize: function(variable) {
this.Var = variable;
},
OL:function(arg){
if(arg==null||arg==undefined) return arg;
if(arg.length!=undefined){
return '<i>Array</i> ('+arg.length+')';
}else{
var count=0;
for(var o in arg){
count++;
}
return '<i>Object</i> ('+count+')';
}
},
dumpO:function(VAr,deepth){
var rtn="";
var prefix=StrR(' ',deepth);
if(VAr==null||VAr==undefined) return prefix+VAr;
if(VAr.length!=undefined){
for(var i=0;i<VAr.length;i++){
rtn+=prefix+'['+i+'] =>{ '+this.dump(VAr[i],deepth+1)+prefix+" }<br/>";
}
}else{
var ct=0;
for(var o in VAr){
rtn+=prefix+'<i>'+o+'</i> =>{ '+this.dump(VAr[o],deepth+1)+prefix+" }<br/>";
ct++;
}
}
return rtn;
},
dump:function(){
var rtn="<pre>";
if(typeof(arguments[0])=='undefined'){
var _var=this.Var;
var deepth=0;
}else{
var _var=arguments[0];
var deepth=arguments[1];
}
var prefix=StrR(' ',deepth);
var type = typeof(_var);
//document.write(type);
switch (type){
case 'string':
case 'number':
case 'boolean':
rtn+=prefix+type+"["+(_var+'').length+"] => "+_var+'<br/>';
break;
case 'object':
rtn+=prefix+this.OL(_var)+' => {<br/>'+this.dumpO(_var,deepth+1)+"<br/>"+prefix+"}<br/>";
break;
default:
rtn+=prefix+type+"["+(_var+'').length+"] => "+_var+' *<br/>';
break;
}
return rtn+'</pre>';
}
}
var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}
}
var StrR=function(str,len){
var Str="";
for(var i=0;i<len;i++)
Str+=str;
return Str;
}
/*
* It is a function has similar function of PHP function var_dump
* @usage:
var v=new VAR(xml);// xml is the variable which you want to print out.
msg=v.dump(); // msg contains the construct of the variable.
*/
var VAR=Class.create();
VAR.prototype = {
initialize: function(variable) {
this.Var = variable;
},
OL:function(arg){
if(arg==null||arg==undefined) return arg;
if(arg.length!=undefined){
return '<i>Array</i> ('+arg.length+')';
}else{
var count=0;
for(var o in arg){
count++;
}
return '<i>Object</i> ('+count+')';
}
},
dumpO:function(VAr,deepth){
var rtn="";
var prefix=StrR(' ',deepth);
if(VAr==null||VAr==undefined) return prefix+VAr;
if(VAr.length!=undefined){
for(var i=0;i<VAr.length;i++){
rtn+=prefix+'['+i+'] =>{ '+this.dump(VAr[i],deepth+1)+prefix+" }<br/>";
}
}else{
var ct=0;
for(var o in VAr){
rtn+=prefix+'<i>'+o+'</i> =>{ '+this.dump(VAr[o],deepth+1)+prefix+" }<br/>";
ct++;
}
}
return rtn;
},
dump:function(){
var rtn="<pre>";
if(typeof(arguments[0])=='undefined'){
var _var=this.Var;
var deepth=0;
}else{
var _var=arguments[0];
var deepth=arguments[1];
}
var prefix=StrR(' ',deepth);
var type = typeof(_var);
//document.write(type);
switch (type){
case 'string':
case 'number':
case 'boolean':
rtn+=prefix+type+"["+(_var+'').length+"] => "+_var+'<br/>';
break;
case 'object':
rtn+=prefix+this.OL(_var)+' => {<br/>'+this.dumpO(_var,deepth+1)+"<br/>"+prefix+"}<br/>";
break;
default:
rtn+=prefix+type+"["+(_var+'').length+"] => "+_var+' *<br/>';
break;
}
return rtn+'</pre>';
}
}
一个例子:
拿我写的那个装载xml到数组的方法(http://tb.blog.csdn.net/TrackBack.aspx?PostId=1806052)装载如下XML(game.xml)到变量xml:
<?xml version="1.0" encoding="utf-8" ?>
<Login>
<Character>
<C Text="热血" Value="0"></C>
<C Text="弱气" Value="1"></C>
<C Text="激情" Value="2"></C>
<C Text="冷静" Value="3"></C>
<C Text="冷酷" Value="4"></C>
</Character>
<Weapon>
<W Text="光束剑" Value="0"></W>
<W Text="光束配刀" Value="1"></W>
</Weapon>
<EconomyProperty>
<P Text="平均型" Value="0"> </P>
<P Text="重视攻击" Value="1"> </P>
<P Text="重视敏捷" Value="2"> </P>
<P Text="重视防御" Value="3"> </P>
<P Text="重视命中" Value="4"> </P>
</EconomyProperty>
</Login>
<Login>
<Character>
<C Text="热血" Value="0"></C>
<C Text="弱气" Value="1"></C>
<C Text="激情" Value="2"></C>
<C Text="冷静" Value="3"></C>
<C Text="冷酷" Value="4"></C>
</Character>
<Weapon>
<W Text="光束剑" Value="0"></W>
<W Text="光束配刀" Value="1"></W>
</Weapon>
<EconomyProperty>
<P Text="平均型" Value="0"> </P>
<P Text="重视攻击" Value="1"> </P>
<P Text="重视敏捷" Value="2"> </P>
<P Text="重视防御" Value="3"> </P>
<P Text="重视命中" Value="4"> </P>
</EconomyProperty>
</Login>
装载的代码:
xml=loadXML('game.xml');
显示变量结构的代码
var v=new VAR(xml);
msg=v.dump();
document.write(msg);
msg=v.dump();
document.write(msg);
结果:
Array (3) => {
[0] =>{Object (3) => {}
$name =>{string[9] => Character}
$value =>{null => {}
null
}
C =>{Array (5) => {}
[0] =>{Object (4) => {}
$name =>{string[1] => C}
$value =>{null => {}
null
}
Text =>{string[2] => 热血}
Value =>{string[1] => 0}
}
[1] =>{Object (4) => {}
$name =>{string[1] => C}
$value =>{null => {}
null
}
Text =>{string[2] => 弱气}
Value =>{string[1] => 1}
}
[2] =>{Object (4) => {}
$name =>{string[1] => C}
$value =>{null => {}
null
}
Text =>{string[2] => 激情}
Value =>{string[1] => 2}
}
[3] =>{Object (4) => {}
$name =>{string[1] => C}
$value =>{null => {}
null
}
Text =>{string[2] => 冷静}
Value =>{string[1] => 3}
}
[4] =>{Object (4) => {}
$name =>{string[1] => C}
$value =>{null => {}
null
}
Text =>{string[2] => 冷酷}
Value =>{string[1] => 4}
}
}
}
[1] =>{Object (3) => {}
$name =>{string[6] => Weapon}
$value =>{null => {}
null
}
W =>{Array (2) => {}
[0] =>{Object (4) => {}
$name =>{string[1] => W}
$value =>{null => {}
null
}
Text =>{string[3] => 光束剑}
Value =>{string[1] => 0}
}
[1] =>{Object (4) => {}
$name =>{string[1] => W}
$value =>{null => {}
null
}
Text =>{string[4] => 光束配刀}
Value =>{string[1] => 1}
}
}
}
[2] =>{Object (3) => {}
$name =>{string[15] => EconomyProperty}
$value =>{null => {}
null
}
P =>{Array (5) => {}
[0] =>{Object (4) => {}
$name =>{string[1] => P}
$value =>{null => {}
null
}
Text =>{string[3] => 平均型}
Value =>{string[1] => 0}
}
[1] =>{Object (4) => {}
$name =>{string[1] => P}
$value =>{null => {}
null
}
Text =>{string[4] => 重视攻击}
Value =>{string[1] => 1}
}
[2] =>{Object (4) => {}
$name =>{string[1] => P}
$value =>{null => {}
null
}
Text =>{string[4] => 重视敏捷}
Value =>{string[1] => 2}
}
[3] =>{Object (4) => {}
$name =>{string[1] => P}
$value =>{null => {}
null
}
Text =>{string[4] => 重视防御}
Value =>{string[1] => 3}
}
[4] =>{Object (4) => {}
$name =>{string[1] => P}
$value =>{null => {}
null
}
Text =>{string[4] => 重视命中}
Value =>{string[1] => 4}
}
}
}
}
参考文章:
javascript解析XML的方法 作者:luke 日期:2007-05-31 URL http://www.lukee.cn/article.asp?id=396