2011-06-27 31 views

回答

0
function getJson(treeNode) { 
    treeNode.expandChildNodes(); 
    var json = {}; 
    var attributes = treeNode.attributes; 
    for(var item in attributes){ 
     if (item == 'src' || item == 'text') { //only use required attributes 
      json[item] = attributes[item]; 
     } 
    } 
    json.children = []; 
    if(treeNode.childNodes.length > 0){ 
     for (var i=0; i < treeNode.childNodes.length; i++) { 
      json.children.push(getJson(treeNode.childNodes[i])); 
     } 
    } 
    return json; 
} 

// To use above function: 
var comp = Ext.getCmp('tree-panel'); //id of the tree panel 
var myJSON = getJson(comp.getRootNode()); 
console.log(Ext.encode(myJSON.children)); 

無法考慮創建XML。我會在服務器端..

希望有人也可以在它上面工作。