把array转成树的方法

小明给的
/**
*
* 树使用的工具
*
* @author   Zhang Ming
* @version  Ver 1.0 2011-03-19 初版
*
*/
package com.ctg.utils.tree
{
import mx.collections.ArrayCollection;
import mx.utils.ObjectUtil;

public class TreeUtil
{
public function TreeUtil()
{
}
public static function arrayToXML(mainArray:Array, subArray:Array = null, keyField:String = null):XMLList{
var xmlList:XMLList;
var tool:ArrayToXMLTool = new ArrayToXMLTool();
var opritionArray:Array = ObjectUtil.copy(mainArray) as Array;

var rootClassinfo:Object = ObjectUtil.getClassInfo(opritionArray);
var xmlString:String = "";
if(subArray != null && keyField != null){
opritionArray = tool.mainArrayConnectionSubArray(opritionArray, subArray, keyField);
}

xmlString += tool.arrayToXMLString(opritionArray);


xmlList = new XMLList(xmlString);
return xmlList;
}
}
}
class ArrayToXMLTool{
import mx.collections.ArrayCollection;
import mx.utils.ObjectUtil;
public function mainArrayConnectionSubArray(mainArray:Array, subArray:Array = null, keyField:String = null):Array{
var array:Array = new Array();
if(keyField == null) return array;
for each(var main:Object in mainArray){
var mainClassinfo:Object = ObjectUtil.getClassInfo(main);
var obj:Object = new Object();
for each(var p:* in mainClassinfo.properties){
obj[p] = main[p];
if(obj[p] is Array){
obj[p] = mainArrayConnectionSubArray(obj[p], subArray, keyField);
break;
} else if(obj[p] is ArrayCollection){
obj[p] = mainArrayConnectionSubArray(obj[p].source, subArray, keyField);
break;
}
}
for each(var sub:Object in subArray){
if(obj[keyField] != null && sub[keyField] == obj[keyField]){
if(obj['zzzz'] == null) obj['zzzz'] = new Array();
obj['zzzz'].push(sub);
}
}
array.push(obj);
}
array.sort(compareFunction);
return array;
}

private function typeToInt(obj:Object):int{
var typeInt:int = 0;
var classinfo:Object = ObjectUtil.getClassInfo(obj);
for each(var p:* in classinfo.properties){
if(obj[p] is Array){
if(obj[p]!= null && obj[p].length > 0) typeInt = 1;
break;
} else if(obj[p] is ArrayCollection){
if(obj[p]!= null && obj[p].length > 0) typeInt = 1;
break;
}
}
return typeInt;
}

private function compareFunction(obj1:Object, obj2:Object):int{
var a:int = typeToInt(obj1);
var b:int = typeToInt(obj2);
if(a>b){
return -1;
} else if(a==b){
return 0;
} else if(a<b){
return 1;
}
return 0;
}
public function arrayToXMLString(array:Array):String{
var xmlString:String = "";
for each(var main:* in array){
var mainClassinfo:Object = ObjectUtil.getClassInfo(main);
xmlString += "<" + "node" + " ";
var stringArray:Array = new Array();
for each(var p:* in mainClassinfo.properties){
if(main[p] is Array){
stringArray.push(arrayToXMLString(main[p]));
} else if(main[p] is ArrayCollection){
stringArray.push(arrayToXMLString(main[p].source));
} else {
xmlString += p.localName + "='" + (main[p]==null?"":main[p]) + "' ";
}
}
xmlString += ">\n" ;
for each(var string:String in stringArray){
xmlString += string;
}
xmlString += "</" + "node" + ">\n";
}
return xmlString;
}

private function getSubArray(object:Object):Array{
return null;
}

}

你可能感兴趣的:(array)