JTree and XML
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Friday, March 22, 2002 11:17 AM

How do U save a JTree as an XML file and construct the tree from that XML file? I'm using the xeres parser.

Re: JTree and XML

Posted By:   Shriram_Ethirajan  
Posted On:   Tuesday, March 26, 2002 12:06 AM

hi,

use the fileoutputstream to save the tree back to file.i will give u a sample code and try it.
.
..
....



else if(e.getActionCommand().equals("Save"))
{try {
fs = new FileOutputStream(fname,false);
callrec();
fs.close();
refreshUI(fname); // build the tree

...
..
.
declare the fileo/pstream in the class
where u make declarations.

callrec() is a method which contains code as ..

public void callrec() {

Object st = m_tree.getModel().getRoot();
System.out.println("
");
System.out.println("Root is ::"+st);
DefaultMutableTreeNode mtn = (DefaultMutableTreeNode)st;
TreeNode tn = mtn.getFirstChild();
System.out.println("first child::"+tn);

try
{
String s = tn.toString()+"
";
byte b[];
b=s.getBytes();
fs.write(b);
}
catch(Exception eq)
{
System.out.println(eq);
}
MutableTreeNode mt = (MutableTreeNode)tn;

recursive(mt); // another method

if (mt.getChildCount() > 0)
{
byte bb[];
String s = mt.toString();
s = "";
bb = s.getBytes();
try {
fs.write(bb);
}
catch(Exception e){}
System.out.println(s);
}

.
..
...
public void recursive(MutableTreeNode tm)
{ try
{
for(int i=0;i {
MutableTreeNode dts = (MutableTreeNode)tm.getChildAt(i);
String s = dts.toString();
System.out.println(s);
byte b[];
b = s.getBytes();
fs.write(b);
if (dts.getChildCount() > 0 )
{
recursive(dts);
}
if (dts.getChildCount() > 0)
{
byte bb[];
s = "";
bb = s.getBytes();
fs.write(bb);
System.out.println(s);
}


}

}//try
catch(Exception ew)
{
ew.printStackTrace();
}
}




if u r not clear mail me.
About | Sitemap | Contact