xml2json-把xml文档转为json对象

var to_string = Object.prototype.toString;
function xml2json(node, src){
    var result = src || {};
    var childs = node.childNodes, child, name;
    for(var i=0;i<childs.length;i++){
        child = childs(i);
        if(child.nodeType == 3 || child.nodeType == 4) return child.text;
        name = child.tagName;
        if(result[name]){
            if(to_string.call(result[name]).indexOf("Array")<0) 
            result[name] = [result[name]];
            result[name].push(xml2json(child));
        }else{
            result[name] = xml2json(child);
        }
    }
    return result;
}

发表评论评论(0)