在 AS3 中使用 tree
http://developer.yahoo.com/flash/astra-flash/tree/XML数据生成,java示例:
TreeData.java
package org.jiucai.ext.yahoo.fl.data;
import java.io.Serializable;
/**
*
* 树形结构的节点数据结构
* <pre>
*
* 类简介
*
* 备注:
* 1.
* 2.
* 3.
* </pre>
*
* @author: zhaidw
* @date: 2010-10-23
*
*
*/
public class TreeData implements Serializable {
private static final long serialVersionUID = 3359824250382904965L;
private Integer id;
private Integer parentId;
private String name;
private Integer treeLevel;
public TreeData() {
super();
}
public TreeData(Integer id, Integer parentId, String name, Integer treeLevel) {
super();
this.id = id;
this.parentId = parentId;
this.name = name;
this.treeLevel = treeLevel;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public Integer getTreeLevel() {
return treeLevel;
}
public void setTreeLevel(Integer treeLevel) {
this.treeLevel = treeLevel;
}
}
Tree.java
package org.jiucai.ext.yahoo.fl.data;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
/**
*
* 树XML数据生成类
* <pre>
*
* 类简介
*
* 备注:
* 1.依赖于 dom4j.jar 库
* 2.
* 3.
* </pre>
*
* @author: zhaidw
* @date: 2010-10-23
*
*
*/
public class Tree {
public static String getXML(List<TreeData> list){
String xml ="";
Document document = DocumentHelper.createDocument();
Element root = document.addElement("node");
root.addAttribute("label", "根节点");
root.addAttribute("id", "");
root.addAttribute("parentId", "");
root.addAttribute("treeLevel", "");
Element baseNode = root.addElement("node");
baseNode.addAttribute("label", "所有");
baseNode.addAttribute("id", "");
baseNode.addAttribute("parentId", "");
baseNode.addAttribute("treeLevel", "");
addSubNode(baseNode , list, 0);
xml = document.getRootElement().asXML();
return xml;
}
private static void addSubNode(Element parentNode , List<TreeData> list, Integer nodeId){
if(null != list && list.size() > 0){
Element node;
TreeData data;
for (int i = 0; i < list.size(); i++) {
data = list.get(i);
if( nodeId == data.getParentId() ){
node = parentNode.addElement("node");
node.addAttribute("label", data.getName());
node.addAttribute("id", data.getId().toString());
node.addAttribute("parentId", data.getParentId().toString());
node.addAttribute("treeLevel", data.getTreeLevel().toString());
addSubNode(node , list, data.getId());
}
}
}
}
public static void main(String[] args) {
List<TreeData> list = new ArrayList<TreeData>();
TreeData data;
data= new TreeData();
data.setId(1);
data.setParentId(0);
data.setName("节点1");
data.setTreeLevel(1);
list.add(data);
data= new TreeData();
data.setId(2);
data.setParentId(0);
data.setName("节点2");
data.setTreeLevel(1);
list.add(data);
data= new TreeData();
data.setId(3);
data.setParentId(1);
data.setName("节点11");
data.setTreeLevel(1);
list.add(data);
String xml = Tree.getXML(list);
System.out.println(xml);
}
}