This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
The code loads all the nodes of the xml into a JTree.Now i was modifying the code so as to display only specific nodes(ie only Take node)and also inside that specific node,a specific node(ie only relevant node).How can i modify the code.Please help
DefaultMutableTreeNode allows you to remove nodes. So instead of modifying that creation code you could just create the full tree and remove what you don't want. Once the XML is in TreeNode form you can iterate over it a lot better.
I myself prefer using a DOM model like JDOM. That gives you immediate control over the entire structure.
I am able to delete the Donttake node of the xml but when i try to delete the key child node .I am getting a null pointer exception.I commented out the lines as shown above and saw that path1 itself is null.Somebody Please help
If I were you I'd do the removal directly on the tree nodes, not through the tree:
Now of course the "Donttake".equals(name) || "key".equals(name) part may change, so you might want to turn that into a new method:
This will make it easier for you to change the filter rules - just change that one method.
You may want to do this prior to creating the tree, or prior to setting the tree model, so you won't have to update your tree afterwards again. [ August 13, 2008: Message edited by: Rob Prime ]
Joined: Jan 04, 2008
thanks rob,but since i am a newbie in swings Programming i am having issues as to where filterNodes function would be called, in the following code.I am not sure where the tree is being created.This code loads all the nodes of xml in JTree.
can you please help [ August 13, 2008: Message edited by: Rob Prime ]
That's where I'd put it. The entire tree has just been built but it isn't used yet, so it's a great opportunity to filter out what you don't need.
Joined: Jan 04, 2008
thanks rob,you solved most of my headache:-)
one more thing that is to be noted is that node.removeFromParent(); removes alternate key child nodes ,not all the child nodes ,something else has to be put here instead of node.removeFromParent() so as to remove all the key child nodes