一个实用的 Javascript XML to JSON Object 对象的转换 (JQuery)

一个实用的 Javascript XML to JSON  Object 对象的转换 (JQuery)

一个用来将xml文件转换成对象的插件

xml文件如下
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<content>
  <plugins>TreeView</plugins>
  <plugins>ListGrid</plugins>
  <plugins>ListBox</plugins>
</content>
<pool>1</pool>
</root>

生成的对象为:



调用

var xml = $.loadXML("PluginConf.xml");
var obj = $(xml).toObject().get(0);
alert(obj.pool);

以下是 源码下载

/* *
 * 转换xml为对象形式
 * @return {Object}
 * @param {XMLHttpRequest} elXML
 
*/
$.fn.toObject 
=   function  (){
    
if  ( this == null return   null ;
    
var  retObj  =   new  Object;
    buildObjectNode(retObj,
/* jQuery */ this .get( 0 ));
    
return  $(retObj);
    
function  buildObjectNode(cycleOBJ, /* Element */ elNode){
        
/* NamedNodeMap */
        
var  nodeAttr = elNode.attributes;
        
if (nodeAttr  !=   null ){
            
if  (nodeAttr.length && cycleOBJ == null ) cycleOBJ = new  Object; 
            
for ( var  i = 0 ;i < nodeAttr.length;i ++ ){
                cycleOBJ[nodeAttr[i].name]
= nodeAttr[i].value;
            }
        }
        
var  nodeText = " text " ;
        
if  (elNode.text == null ) nodeText = " textContent " ;
        
/* NodeList */
        
var  nodeChilds = elNode.childNodes;
        
if (nodeChilds != null ){
            
if  (nodeChilds.length && cycleOBJ == null ) cycleOBJ = new  Object; 
            
for ( var  i = 0 ;i < nodeChilds.length;i ++ ){
                
if  (nodeChilds[i].tagName != null ){
                    
if  (nodeChilds[i].childNodes[ 0 ] != null && nodeChilds[i].childNodes.length <= 1 && (nodeChilds[i].childNodes[ 0 ].nodeType == 3 || nodeChilds[i].childNodes[ 0 ].nodeType == 4 )){
                        
if  (cycleOBJ[nodeChilds[i].tagName] == null ){
                            cycleOBJ[nodeChilds[i].tagName]
= nodeChilds[i][nodeText];
                        }
else {
                            
if  ( typeof (cycleOBJ[nodeChilds[i].tagName]) == " object " && cycleOBJ[nodeChilds[i].tagName].length){
                                cycleOBJ[nodeChilds[i].tagName][cycleOBJ[nodeChilds[i].tagName].length]
= nodeChilds[i][nodeText];
                            }
else {
                                cycleOBJ[nodeChilds[i].tagName]
= [cycleOBJ[nodeChilds[i].tagName]];
                                cycleOBJ[nodeChilds[i].tagName][
1 ] = nodeChilds[i][nodeText];
                            }
                        }
                    }
else {
                        
if  (nodeChilds[i].childNodes.length){
                            
if  (cycleOBJ[nodeChilds[i].tagName] == null ){
                                cycleOBJ[nodeChilds[i].tagName]
= new  Object;
                                buildObjectNode(cycleOBJ[nodeChilds[i].tagName],nodeChilds[i]);
                            }
else {
                                
if  (cycleOBJ[nodeChilds[i].tagName].length){
                                    cycleOBJ[nodeChilds[i].tagName][cycleOBJ[nodeChilds[i].tagName].length]
= new  Object;
                                    buildObjectNode(cycleOBJ[nodeChilds[i].tagName][cycleOBJ[nodeChilds[i].tagName].length
- 1 ],nodeChilds[i]);
                                }
else {
                                    cycleOBJ[nodeChilds[i].tagName]
= [cycleOBJ[nodeChilds[i].tagName]];
                                    cycleOBJ[nodeChilds[i].tagName][
1 ] = new  Object;
                                    buildObjectNode(cycleOBJ[nodeChilds[i].tagName][
1 ],nodeChilds[i]);
                                }
                            }
                        }
else {
                            cycleOBJ[nodeChilds[i].tagName]
= nodeChilds[i][nodeText];
                        }
                    }
                }
            }
        }
    }
}

/* *
 * @return {Element}
 * @param {String} _url
 
*/
$.loadXML 
=   function  (_url){
    
var  ret;
    $.ajax({
          type:
" get " ,
          url:_url,
        async:
false ,
          dataType:
" xml " ,
        success:
function (xml){
            ret 
=  xml;
        }
      });
    
return  ret.documentElement;
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值