The program is mainly assembled in JSON-formatted tree data files, divided into two parts:
A simple pojo that represents a node
public class TreeNode {
Private long nodeId;
Private String nodename;
Private long Father_nodeid;
Public TreeNode () {}
Public TreeNode (Long nodeid,string nodename,long Father_nodeid) {
This.nodeid=nodeid;
This.nodename=nodename;
This.father_nodeid=father_nodeid;
}
Public long Getnodeid () {
return nodeId;
}
public void Setnodeid (long nodeId) {
This.nodeid = nodeId;
}
Public String Getnodename () {
return nodename;
}
public void Setnodename (String nodename) {
This.nodename = nodename;
}
Public long Getfather_nodeid () {
return Father_nodeid;
}
public void Setfather_nodeid (long Fathernodeid) {
Father_nodeid = Fathernodeid;
}
}
The tool class of the two building trees
public class Constructortree {
StringBuffer json=new StringBuffer ();
/**
* Build JSON file
* @param list
* @param node
*/
public void Constructorjson (list<treenode> List, TreeNode TreeNode) {
if (Haschild (List,treenode)) {
Json.append ("{/" Id/":/");
Json.append (Treenode.getnodeid () + "/");
Json.append (",/" text/":/");
Json.append (Treenode.getnodename () + "/");
Json.append (",/" children/": [");
list<treenode> childlist = getchildlist (List,treenode);
Iterator iterator = Childlist.iterator ();
while (Iterator.hasnext ()) {
TreeNode node = (TreeNode) iterator.next ();
Constructorjson (List,node);
}
Json.append ("]},");
}else{
Json.append ("{/" Id/":/");
Json.append (Treenode.getnodeid () + "/");
Json.append (",/" text/":/");
Json.append (Treenode.getnodename () + "/");
Json.append ("},");
}
}
/**
* Get sub-node list information
* @param list
* @param node
* @return
*/
Public list<treenode> getchildlist (list<treenode> list, TreeNode node) {//Get child node list
list<treenode> li = new arraylist<treenode> ();
Iterator it = List.iterator ();
while (It.hasnext ()) {
TreeNode n = (TreeNode) it.next ();
if (N.getfather_nodeid () ==node.getnodeid ()) {
Li.add (n);
}
}
return Li;
}
/**
* Determine if there are child nodes
* @param list
* @param node
* @return
*/
public boolean haschild (list<treenode> List, TreeNode node) {
Return Getchildlist (List,node). Size () >0?true:false;
}
Public String Getjson (list<treenode> List, TreeNode node) {
JSON = new StringBuffer ();
Constructorjson (List,node);
String jsondate=json.tostring ();
Return ("[" +jsondate+ "]"). ReplaceAll (",]", "]");
}
}