一个类似与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('&nbsp;&nbsp;',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('&nbsp;&nbsp;',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 >
        
< Text ="热血"  Value ="0" ></ C >
        
< Text ="弱气"  Value ="1" ></ C >
        
< Text ="激情"  Value ="2" ></ C >
        
< Text ="冷静"  Value ="3" ></ C >
        
< Text ="冷酷"  Value ="4" ></ C >
    
</ Character >
    
< Weapon >
        
< Text ="光束剑"  Value ="0" ></ W >
        
< Text ="光束配刀"  Value ="1" ></ W >
    
</ Weapon >
    
< EconomyProperty >
        
< Text ="平均型"  Value ="0" > &nbsp; </ P >
        
< Text ="重视攻击"  Value ="1" > &nbsp; </ P >
        
< Text ="重视敏捷"  Value ="2" > &nbsp; </ P >
        
< Text ="重视防御"  Value ="3" > &nbsp; </ P >
        
< Text ="重视命中"  Value ="4" > &nbsp; </ P >
    
</ EconomyProperty >
</ Login >

装载的代码: 

xml=loadXML('game.xml');

显示变量结构的代码

var v=new VAR(xml);
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

转载于:https://www.cnblogs.com/Iamlein/archive/2007/09/29/2375955.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值