|
Hello,
I am trying to traverse and tree and want to assign the node id for each node in the tree.
It would be great if someone helps me with it.
Here is the code which i am using and the problem is the nodes in the tree are repeating everytime and i want to visit each node only once and assign the node id to it.
protected void walk(TreeModel model, Object o){
int cc;
int x=1;
cc = model.getChildCount(o);
System.out.println(" childcount ==> "+cc);
for( int i=0; i < cc; i++) {
Object child = model.getChild(o, i );
if (model.isLeaf(child)){
System.out.println(" Leaf ==> "+child.toString());
TreeNode[] pathToRoot = ((DefaultMutableTreeNode)child).getPath();
System.out.println("Path==> "+((DefaultMutableTreeNode)child).getPath().toStri ng());
for (int j= 1; j < (pathToRoot.length)-1 ; j++ )
{
System.out.print(" ==> "+pathToRoot[j]);
System.out.print("insert into Node (Doc_ID, PNode_ID, Element) values ("+x +" ,"+ y +" , '" + pathToRoot[j] + "')");
storeDB("insert into Node (Doc_ID, PNode_ID, Element) values ('" + x +"' ,'"+ y +"' , '" + pathToRoot[j] + "')");
System.out.println("");
y++;
}
}
else {
System.out.print(child.toString()+"--");
walk(model,child );
}
}
}
public void storeDB (String insertSQL){
try {
Statement st = con.createStatement();
System.out.println("inserting***");
st.executeUpdate(insertSQL);
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
|