• Post Reply Bookmark Topic Watch Topic
  • New Topic

i need to create a xml file from Jtree

 
sudheer kiran
Ranch Hand
Posts: 237
Firefox Browser MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all i have a JTree.
i want to create a xml file from the JTree with the user objects of defaultruntimeTreenode.

lets suppose there is a parent and child node.

i want my xml file to be like this

<parent>

<name> firstname</name>
<age> 12 </age>

<child>
<name> childname</name>
<age> 5 </age>
<interest> sports </sports>
</child>
</parent>

is that possible..please suggest.

thanks.
 
Arun Chidam
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sudheer kiran wrote:hi all i have a JTree.
i want to create a xml file from the JTree with the user objects of defaultruntimeTreenode.

lets suppose there is a parent and child node.

i want my xml file to be like this

<parent>

<name> firstname</name>
<age> 12 </age>

<child>
<name> childname</name>
<age> 5 </age>
<interest> sports </sports>
</child>
</parent>

is that possible..please suggest.

thanks.


Sudheer,

Try the below code

private static Element createTree(Document doc, TreeModel model, Object node) {
Element el = doc.createElement(node.toString());
for(int i=0;i<model.getChildCount(node);i++){
Object child = model.getChild(node, i);
el.appendChild(createTree(doc,model,child));
}
return el;
}

This method will return the list of elements from JTree, then you use JDOM Document object to append the root element

To convert to string pass the document object to DOMSource and using Transformerfactory you can transfor to string.

>
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!