Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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.

>
 
reply
    Bookmark Topic Watch Topic
  • New Topic