| Author |
Loading Specific nodes of XML into a JTree
|
Ajay Singh
Ranch Hand
Joined: Jan 04, 2008
Posts: 105
|
|
Hi all i was executing the code(VSX.java) given in http://forums.sun.com/thread.jspa?messageID=10184400 and giving the input file(testfile.xml) as <Root> <Take ID="8799">A <key >1</key> <key weight="0.2">2</key> </Take> <Take ID="8789">B <key weight ="0.4">1</key> <key weight="0.1">2</key> <relevant weight="0.1">w2</relevant> </Take> <Take ID="8489">C <key weight ="0.5">1</key> <relevant weight="0.6">w2</relevant> </Take> <Donttake>D <keys weight ="0.5">1</keys> </Donttake> </Root> 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
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Ajay Singh
Ranch Hand
Joined: Jan 04, 2008
Posts: 105
|
|
thanks Rob for the reply Based on your suggestion ,i am now loading the Every node of xml into a JTree and now i am removing the irrelevant nodes using the following code model = (DefaultTreeModel)tree.getModel(); path = tree.getNextMatch("Donttake", 0, Position.Bias.Forward); path1 = tree.getNextMatch("key", 0, Position.Bias.Forward); System.out.println(path+"........"+path1); mNode = (MutableTreeNode)path.getLastPathComponent(); // mNode1=(MutableTreeNode)path1.getLastPathComponent(); model.removeNodeFromParent(mNode); // model.removeNodeFromParent(mNode1); 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
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
Is the key node visible, or is it hidden? 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 ]
|
 |
Ajay Singh
Ranch Hand
Joined: Jan 04, 2008
Posts: 105
|
|
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 ]
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
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.
|
 |
Ajay Singh
Ranch Hand
Joined: Jan 04, 2008
Posts: 105
|
|
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
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
You can remove a node from its parent by calling remove(int) or remove(MutableTreeNode) on the parent. Notice how I iterate backwords; that's a quite common trick when removing elements. If you remove from the back, the indexes of the previous elements won't change.
|
 |
 |
|
|
subject: Loading Specific nodes of XML into a JTree
|
|
|