• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

i need to create a xml file from Jtree

 
Ranch Hand
Posts: 237
MyEclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

>
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic