Hi,

I am converting a given html document into a tree structure and displaying it using JTree and DefaultMutable classes.

By using the html parser and the JTree functionality, I can display the html doc as tree structure, but now I would like to get the subtrees only of that tree when clicked on any of the nodes in the tree or else If I input the node, I need to display only the subtree associated with that node and not the entire tree. Is there any function to do that or else if I need to parse with along the tree structure, How can I do that and what functions and classes are available for that.

Any help is appreciated.

Thanks!!!

Recommended Answers

All 10 Replies

I'm not clear on what you want to do with the subtrees when you say "get the subtrees". If you select a node, you essentially have that subtree via it's children collection. What you do with that collection depends on your needs, so perhaps a bit more info would help.

import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.util.Iterator;
import java.util.List;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import java.io.File;

public class XMLTreeViewer extends JFrame {

  //The JTree to display the XML
  private JTree xmlTree;

  //The XML document to be output to the JTree
  private Document xmlDoc;
  DefaultMutableTreeNode tn;
  
  public XMLTreeViewer(Document doc) {
    super();
    this.xmlDoc = doc;
    setSize(600, 450);
    tn = new DefaultMutableTreeNode("Hyper Tree");
    initialize();
  }

  private void initialize() {
    xmlTree = new JTree();
    xmlTree.setName("Hyper Tree");
    getContentPane().add(new JScrollPane(xmlTree), BorderLayout.CENTER);
   // getContentPane().add(new JPanel());
    processElement(xmlDoc.getRootElement(), tn);
    ( (DefaultTreeModel) xmlTree.getModel()).setRoot(tn);
    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent e) {
        //release all the resource
        xmlTree = null;
        tn = null;
      }
    });
    setVisible(true);
    //add Listener to Print to Screen the xml tag selected
    xmlTree.addTreeSelectionListener(new TreeSelectionListener() {
      public void valueChanged(TreeSelectionEvent evt) {
        // Get all nodes whose selection status has changed
        TreePath[] paths = evt.getPaths();
        // Print the last Path Component selected
        System.out.println(evt.getPath().getLastPathComponent());

//print the full path from the selected tag
        // System.out.println(evt.getPath().toString());
      }
    });
  }

  private void processElement(Element el, DefaultMutableTreeNode dmtn) {
    DefaultMutableTreeNode currentNode =  new DefaultMutableTreeNode(el.getName());
    String text = el.getTextNormalize();
    if ( (text != null) && (!text.equals(""))) {
      currentNode.add(new DefaultMutableTreeNode(text));
    }
    //processAttributes(el, currentNode);

    Iterator children = el.getChildren().iterator();
    while (children.hasNext()) {
      processElement( (Element) children.next(), currentNode);
    }
    dmtn.add(currentNode);
  }

  private void processAttributes(Element el, DefaultMutableTreeNode dmtn) {
    Iterator atts = el.getAttributes().iterator();

    while (atts.hasNext()) {
      Attribute att = (Attribute) atts.next();
      DefaultMutableTreeNode attNode = new DefaultMutableTreeNode("@" + att.getName());
      attNode.add(new DefaultMutableTreeNode(att.getValue()));
      dmtn.add(attNode);
    }
  }

  public static void main(String args[]) throws Exception {
    SAXBuilder builder = new SAXBuilder();
    //  Document doc = builder.build(new File("mails.xsd"));
    Document doc = builder.build(new File("C://Documents and Settings//cs.html"));
    XMLTreeViewer viewer = new XMLTreeViewer(doc);
    viewer.addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent e) {
        System.exit(0);
      }
    });
  }
}

By using the above code, I am able to read an html document and display the resultant as a tree. Now I would like to define some operators on it.
In the tree structure, I would like to display only subtrees that I want. For example, if the root node is "html" and it has 2 children "body" and "head" then if I select the node "body" then i want to display only the subtree associated with the node "body" and so on.

So I would like to know if there are any predefined functions for tree traversal or if any other links that might help me out to get the desired result.

Thanks!!

I'm still not exactly clear on the behavior you are wanting. You wish to replace the displayed tree with the selected subtree? Or you want to present that subtree in another location separate from the main document tree?

If you want to replace the existing displayed tree with a selected subtree, you can change your selection listenter to do this

//add Listener to Print to Screen the xml tag selected
    xmlTree.addTreeSelectionListener(new TreeSelectionListener() {
      public void valueChanged(TreeSelectionEvent evt) {
        // Get all nodes whose selection status has changed
        TreePath[] paths = evt.getPaths();
        // Print the last Path Component selected
        System.out.println(evt.getPath().getLastPathComponent());

[B]         DefaultTreeModel newModel = new DefaultTreeModel((TreeNode)evt.getPath().getLastPathComponent());
        xmlTree.setModel(newModel);[/B]
        
        
//print the full path from the selected tag
        // System.out.println(evt.getPath().toString());
      }
    });

That of course discards the overall document model unless you retain that as a separate reference, which you would need if you wanted any way to go back after navigation.

I'm not sure if that is what you are wanting, but maybe it gets you close. If it totally misses what you are wanting to do, post a little more detail on what you wish to do with the selected subtree.

Hi,

Thanks for the reply. But by doing the above I will be able to modify the original tree that I have and will not be able to display the original tree that I have.

How can I display the subtree in another window or frame such that my original tree is preserved??

Please help me out in this.....

Thanks!!!

Hi,

Thanks for the reply. But by doing the above I will be able to modify the original tree that I have and will not be able to display the original tree that I have.

How can I display the subtree in another window or frame such that my original tree is preserved??

Please help me out in this.....

Thanks!!!

Just create a new JTree component and use the code above to create it's model. In my example, I replaced the existing model the new one. You can just as easily keep the original and work with multiple JTrees.

[B]import[/B] java.awt.*;

[B]import[/B] javax.swing.*;
[B]import[/B] javax.swing.tree.*;
[B]import[/B] javax.swing.event.*;

[B]import[/B] java.util.Iterator;
[B]import[/B] java.util.List;
[B]import[/B] org.jdom.*;
[B]import[/B] org.jdom.input.SAXBuilder;
[B]import[/B] java.io.File;
[B]import[/B] java.awt.event.*;

[B]public[/B] [B]class[/B] XMLTreeViewer [B]extends[/B] JFrame [B]implements[/B] ActionListener{

//The JTree to display the XML
[B]private[/B] JTree xmlTree;
[B]private[/B] JTree resultTree;
JButton showButton ;
JButton primeButton;

//The XML document to be output to the JTree
[B]private[/B] Document xmlDoc;
DefaultMutableTreeNode tn,selectedNode;

[B]public[/B] XMLTreeViewer(Document doc) {
[B]super[/B]("WebOQL Homework");
[B]this[/B].xmlDoc = doc; //document
setSize(600, 450);
tn = [B]new[/B] DefaultMutableTreeNode("Hyper Tree");
initialize();
}

[B]private[/B] [B]void[/B] initialize() {
xmlTree = [B]new[/B] JTree();
xmlTree.setName("HyperTree");
getContentPane().add([B]new[/B] JScrollPane(xmlTree), BorderLayout.[I]LINE_START[/I]);
getContentPane().add(tabbedPane(),BorderLayout.[I]LINE_END[/I]);
processElement(xmlDoc.getRootElement(), tn); //create a tree
( (DefaultTreeModel) xmlTree.getModel()).setRoot(tn);
addWindowListener([B]new[/B] java.awt.event.WindowAdapter() {
[B]public[/B] [B]void[/B] windowClosing(java.awt.event.WindowEvent e) {
//release all the resource
xmlTree = [B]null[/B];
tn = [B]null[/B];
}
});
setVisible([B]true[/B]);

//add Listener to Print to Screen the xml tag selected
xmlTree.addTreeSelectionListener([B]new[/B] TreeSelectionListener() {
[B]public[/B] [B]void[/B] valueChanged(TreeSelectionEvent evt) {
// Get all nodes whose selection status has changed
TreePath[] paths = evt.getPaths();
// Print the last Path Component selected
System.[I]out[/I].println("Path = " + evt.getPath().getLastPathComponent());
selectedNode = (DefaultMutableTreeNode) xmlTree.getLastSelectedPathComponent();
}
});
}

//Get Tag and element content 
[B]private[/B] [B]void[/B] processElement(Element el, DefaultMutableTreeNode dmtn) {
DefaultMutableTreeNode currentNode = [B]new[/B] DefaultMutableTreeNode(el.getName());
String text = el.getTextNormalize();

[B]if[/B] ( (text != [B]null[/B]) && (!text.equals(""))) {
currentNode.add([B]new[/B] DefaultMutableTreeNode(text));
}


Iterator children = el.getChildren().iterator();
[B]while[/B] (children.hasNext()) {
processElement( (Element) children.next(), currentNode);
}
dmtn.add(currentNode);
}

[B]private[/B] [B]void[/B] processAttributes(Element el, DefaultMutableTreeNode dmtn) {
Iterator atts = el.getAttributes().iterator();

[B]while[/B] (atts.hasNext()) {
Attribute att = (Attribute) atts.next();
DefaultMutableTreeNode attNode = [B]new[/B] DefaultMutableTreeNode("@" + att.getName());
attNode.add([B]new[/B] DefaultMutableTreeNode(att.getValue()));
dmtn.add(attNode);
}
}

[B]private[/B] JTabbedPane tabbedPane(){
JTabbedPane tp = [B]new[/B] JTabbedPane (JTabbedPane.[I]LEFT[/I]);
tp.addTab("Operators", createPage1());

[B]return[/B] tp;
}

[B]private[/B] JPanel createPage1(){
JPanel pg = [B]new[/B] JPanel();

Box top = [B]new[/B] Box(BoxLayout.[I]Y_AXIS[/I]);



primeButton = [B]new[/B] JButton("Prime(')");
primeButton.addActionListener([B]this[/B]);

top.add(primeButton);
pg.add(top);
[B]return[/B] pg;
}

[B]public[/B] [B]void[/B] actionPerformed(ActionEvent e){
DefaultMutableTreeNode tn1;
/*** 1. Prime***/
[B]if[/B](e.getSource().equals(showButton)){
System.[I]out[/I].println("showButton is clicked.");
tn1 = [B]new[/B] DefaultMutableTreeNode("Result: Hyper Tree");

processPrime(xmlDoc.getRootElement(), tn1, 1); 

resultTree = [B]new[/B] JTree();
( (DefaultTreeModel) resultTree.getModel()).setRoot(tn1);
getContentPane().add([B]new[/B] JScrollPane(resultTree), BorderLayout.[I]CENTER[/I]);

setVisible([B]true[/B]);


}
/*** 2. Prime from the tree ***/ 
[B]else[/B] [B]if[/B](e.getSource().equals(primeButton)){

System.[I]out[/I].println("primeButton is clicked.");
tn1 = [B]new[/B] DefaultMutableTreeNode("Result: Hyper Tree");
//DefaultMutableTreeNode cloneNode = (DefaultMutableTreeNode) selectedNode.clone();
[B]int[/B] childCount = selectedNode.getChildCount();
System.[I]out[/I].println("loops = "+ childCount);
DefaultMutableTreeNode newChild = [B]new[/B] DefaultMutableTreeNode();
[B]for[/B]([B]int[/B] i = 0; i < childCount; i++){
newChild = (DefaultMutableTreeNode)( selectedNode.getChildAt(0));
tn1.add(newChild);


} 

resultTree = [B]new[/B] JTree();
( (DefaultTreeModel) resultTree.getModel()).setRoot(tn1); 
getContentPane().add([B]new[/B] JScrollPane(resultTree), BorderLayout.[I]CENTER[/I]);
setVisible([B]true[/B]);

} 
}

[B]private[/B] [B]void[/B] processPrime(Element el, DefaultMutableTreeNode dmtn, [B]int[/B] lev) {

Iterator children = el.getChildren().iterator();
[B]while[/B] (children.hasNext()) {


Object childObject = children.next();
DefaultMutableTreeNode currentNode = [B]new[/B] DefaultMutableTreeNode(((Element) childObject).getName());
processElement( (Element) childObject, dmtn);

} 
lev--; 
}

[B]private[/B] [B]void[/B] processTail(Element el, DefaultMutableTreeNode dmtn, [B]int[/B] lev) {

lev--; 
}


[B]public[/B] [B]static[/B] [B]void[/B] main(String args[]) [B]throws[/B] Exception {
SAXBuilder builder = [B]new[/B] SAXBuilder();
Document doc = builder.build([B]new[/B] File("C://Users//Brugu Maharishi//Documents//Google Talk Received Files//cs3.html"));
XMLTreeViewer viewer = [B]new[/B] XMLTreeViewer(doc);
viewer.addWindowListener([B]new[/B] java.awt.event.WindowAdapter() {
[B]public[/B] [B]void[/B] windowClosing(java.awt.event.WindowEvent e) {
System.[I]exit[/I](0);
}
});
}
}

I have declared an operator called as "prime" which will display the subtree of the selected node in the original tree in a new frame. But when I am performing this operator on the original tree, it is also modifying the original tree thus preventing me to perform the same operation on the same node. I need to restart the application in order to perform the operation again on the same node.

Can you plese let me know where I am going wrong or where to modify the code such that performing the operation on the original tree doesnt modify it.

Thanks!!

Sorry......the code given above could not display properly. So I am resending the code back in proper form.

import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.util.Iterator;
import java.util.List;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import java.io.File;
import java.awt.event.*;
public class XMLTreeViewer extends JFrame implements ActionListener{
  //The JTree to display the XML
  private JTree xmlTree;
  private JTree resultTree;
  JButton showButton ;
  JButton primeButton;
 
//The XML document to be output to the JTree
  private Document xmlDoc;
  DefaultMutableTreeNode tn,selectedNode;
 
  public XMLTreeViewer(Document doc) {
    super("WebOQL Homework");
    this.xmlDoc = doc;   //document
    setSize(600, 450);
    tn = new DefaultMutableTreeNode("Hyper Tree");
    initialize();
  }
 
  private void initialize() {
     xmlTree = new JTree();
     xmlTree.setName("HyperTree");
     getContentPane().add(new JScrollPane(xmlTree), BorderLayout.LINE_START);
     getContentPane().add(tabbedPane(),BorderLayout.LINE_END);
     processElement(xmlDoc.getRootElement(), tn);   //create a tree
     ( (DefaultTreeModel) xmlTree.getModel()).setRoot(tn);
     addWindowListener(new java.awt.event.WindowAdapter() {
       public void windowClosing(java.awt.event.WindowEvent e) {
         //release all the resource
         xmlTree = null;
         tn = null;
       }
     });
     setVisible(true);
 
     //add Listener to Print to Screen the xml tag selected
     xmlTree.addTreeSelectionListener(new TreeSelectionListener() {
       public void valueChanged(TreeSelectionEvent evt) {
         // Get all nodes whose selection status has changed
         TreePath[] paths = evt.getPaths();
         // Print the last Path Component selected
         System.out.println("Path = " + evt.getPath().getLastPathComponent());
         selectedNode = (DefaultMutableTreeNode) xmlTree.getLastSelectedPathComponent();
       }
     });
   }
 
//Get Tag and element content  
  private void processElement(Element el, DefaultMutableTreeNode dmtn) {
    DefaultMutableTreeNode currentNode =  new DefaultMutableTreeNode(el.getName());
    String text = el.getTextNormalize();
    if ( (text != null) && (!text.equals(""))) {
      currentNode.add(new DefaultMutableTreeNode(text));
    }
 
 
    Iterator children = el.getChildren().iterator();
    while (children.hasNext()) {
      processElement( (Element) children.next(), currentNode);
    }
    dmtn.add(currentNode);
  }
  private void processAttributes(Element el, DefaultMutableTreeNode dmtn) {
    Iterator atts = el.getAttributes().iterator();
    while (atts.hasNext()) {
      Attribute att = (Attribute) atts.next();
      DefaultMutableTreeNode attNode = new DefaultMutableTreeNode("@" + att.getName());
      attNode.add(new DefaultMutableTreeNode(att.getValue()));
      dmtn.add(attNode);
    }
  }
 
  private JTabbedPane tabbedPane(){
   JTabbedPane tp = new JTabbedPane (JTabbedPane.LEFT);
   tp.addTab("Operators", createPage1());
 
   return tp;
  }
 
  private JPanel createPage1(){
   JPanel pg = new JPanel();
 
   Box top = new Box(BoxLayout.Y_AXIS);
 
 
 
  primeButton = new JButton("Prime(')");
  primeButton.addActionListener(this);
 
  top.add(primeButton);
  pg.add(top);
    return pg;
  }
 
  public void actionPerformed(ActionEvent e){
   DefaultMutableTreeNode tn1;
 /*** 1. Prime***/
 if(e.getSource().equals(showButton)){
  System.out.println("showButton is clicked.");
  tn1 = new DefaultMutableTreeNode("Result: Hyper Tree");
 
  processPrime(xmlDoc.getRootElement(), tn1, 1); 
 
  resultTree = new JTree();
  ( (DefaultTreeModel) resultTree.getModel()).setRoot(tn1);
  getContentPane().add(new JScrollPane(resultTree), BorderLayout.CENTER);
 
  setVisible(true);
 
 
 }
 /*** 2. Prime from the tree ***/ 
 else if(e.getSource().equals(primeButton)){
 
  System.out.println("primeButton is clicked.");
  tn1 = new DefaultMutableTreeNode("Result: Hyper Tree");
  //DefaultMutableTreeNode cloneNode = (DefaultMutableTreeNode) selectedNode.clone();
  int childCount = selectedNode.getChildCount();
  System.out.println("loops = "+ childCount);
  DefaultMutableTreeNode newChild = new DefaultMutableTreeNode();
  for(int i = 0; i < childCount; i++){
   newChild = (DefaultMutableTreeNode)( selectedNode.getChildAt(0));
   tn1.add(newChild);
 
  } 
 
  resultTree = new JTree();
  ( (DefaultTreeModel) resultTree.getModel()).setRoot(tn1);  
  getContentPane().add(new JScrollPane(resultTree), BorderLayout.CENTER);
  setVisible(true);
 
 } 
  }
 
 private void processPrime(Element el, DefaultMutableTreeNode dmtn, int lev) {
     Iterator children = el.getChildren().iterator();
     while (children.hasNext()) {
 
 
      Object childObject = children.next();
         DefaultMutableTreeNode currentNode =  new DefaultMutableTreeNode(((Element) childObject).getName());
         processElement( (Element) childObject, dmtn);
 
       }    
     lev--;    
   }
 
 private void processTail(Element el, DefaultMutableTreeNode dmtn, int lev) {
 
      lev--;    
    }
 
    public static void main(String args[]) throws Exception {
      SAXBuilder builder = new SAXBuilder();
      Document doc = builder.build(new File("C://Users//Brugu Maharishi//Documents//Google Talk Received Files//cs3.html"));
      XMLTreeViewer viewer = new XMLTreeViewer(doc);
      viewer.addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(java.awt.event.WindowEvent e) {
          System.exit(0);
        }
      });
    }
  }

The modification is occurring because you are adding all of the child nodes of the selected node to a new root. The add(MutableTreeNode) method states that it disconnects the added node from its parent and makes it a child of the node it's added to. I see that you tried to use clone() on the selected node, but that fails because they implemented clone() as a shallow copy operation. It does not copy the parent nor children references (deep copy).

How to fix it depends on what you intend to do with the "prime" subtree. If you do not need to modify it without changing the original tree and you can do without the new root node "ResultTree", then the changes I put in bold in the code below will work for you.

If you absolutely need a separate copy of that structure, then you will have to write your own deep copy method that clones a node recursively by cloning that node and its children and all subsequent children of those children. That's not really hard to do, but if you don't need the separate copy you don't need to bother with it.

Sorry......the code given above could not display properly. So I am resending the code back in proper form.

import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.util.Iterator;
import java.util.List;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import java.io.File;
import java.awt.event.*;
public class XMLTreeViewer extends JFrame implements ActionListener{
  //The JTree to display the XML
  private JTree xmlTree;
  private JTree resultTree;
  JButton showButton ;
  JButton primeButton;
 
//The XML document to be output to the JTree
  private Document xmlDoc;
  DefaultMutableTreeNode tn,selectedNode;
 
  public XMLTreeViewer(Document doc) {
    super("WebOQL Homework");
    this.xmlDoc = doc;   //document
    setSize(600, 450);
    tn = new DefaultMutableTreeNode("Hyper Tree");
    initialize();
  }
 
  private void initialize() {
     xmlTree = new JTree();
     xmlTree.setName("HyperTree");
     getContentPane().add(new JScrollPane(xmlTree), BorderLayout.LINE_START);
     getContentPane().add(tabbedPane(),BorderLayout.LINE_END);
     processElement(xmlDoc.getRootElement(), tn);   //create a tree
     ( (DefaultTreeModel) xmlTree.getModel()).setRoot(tn);
     addWindowListener(new java.awt.event.WindowAdapter() {
       public void windowClosing(java.awt.event.WindowEvent e) {
         //release all the resource
         xmlTree = null;
         tn = null;
       }
     });
     setVisible(true);
 
     //add Listener to Print to Screen the xml tag selected
     xmlTree.addTreeSelectionListener(new TreeSelectionListener() {
       public void valueChanged(TreeSelectionEvent evt) {
         // Get all nodes whose selection status has changed
         TreePath[] paths = evt.getPaths();
         // Print the last Path Component selected
         System.out.println("Path = " + evt.getPath().getLastPathComponent());
         selectedNode = (DefaultMutableTreeNode) xmlTree.getLastSelectedPathComponent();
       }
     });
   }
 
//Get Tag and element content  
  private void processElement(Element el, DefaultMutableTreeNode dmtn) {
    DefaultMutableTreeNode currentNode =  new DefaultMutableTreeNode(el.getName());
    String text = el.getTextNormalize();
    if ( (text != null) && (!text.equals(""))) {
      currentNode.add(new DefaultMutableTreeNode(text));
    }
 
 
    Iterator children = el.getChildren().iterator();
    while (children.hasNext()) {
      processElement( (Element) children.next(), currentNode);
    }
    dmtn.add(currentNode);
  }
  private void processAttributes(Element el, DefaultMutableTreeNode dmtn) {
    Iterator atts = el.getAttributes().iterator();
    while (atts.hasNext()) {
      Attribute att = (Attribute) atts.next();
      DefaultMutableTreeNode attNode = new DefaultMutableTreeNode("@" + att.getName());
      attNode.add(new DefaultMutableTreeNode(att.getValue()));
      dmtn.add(attNode);
    }
  }
 
  private JTabbedPane tabbedPane(){
   JTabbedPane tp = new JTabbedPane (JTabbedPane.LEFT);
   tp.addTab("Operators", createPage1());
 
   return tp;
  }
 
  private JPanel createPage1(){
   JPanel pg = new JPanel();
 
   Box top = new Box(BoxLayout.Y_AXIS);
 
 
 
  primeButton = new JButton("Prime(')");
  primeButton.addActionListener(this);
 
  top.add(primeButton);
  pg.add(top);
    return pg;
  }
 
  public void actionPerformed(ActionEvent e){
   DefaultMutableTreeNode tn1;
 /*** 1. Prime***/
 if(e.getSource().equals(showButton)){
  System.out.println("showButton is clicked.");
  tn1 = new DefaultMutableTreeNode("Result: Hyper Tree");
 
  processPrime(xmlDoc.getRootElement(), tn1, 1); 
 
  resultTree = new JTree();
  ( (DefaultTreeModel) resultTree.getModel()).setRoot(tn1);
  getContentPane().add(new JScrollPane(resultTree), BorderLayout.CENTER);
 
  setVisible(true);
 
 
 }
 /*** 2. Prime from the tree ***/ 
 else if(e.getSource().equals(primeButton)){
[B]   // none of this is needed any longer
//       System.out.println("primeButton is clicked.");
//       tn1 = new DefaultMutableTreeNode("Result: Hyper Tree");
//       //DefaultMutableTreeNode cloneNode = (DefaultMutableTreeNode) selectedNode.clone();
//       int childCount = selectedNode.getChildCount();
//  System.out.println("loops = "+ childCount);
//  DefaultMutableTreeNode newChild = new DefaultMutableTreeNode();
//  for(int i = 0; i < childCount; i++){
//   newChild = (DefaultMutableTreeNode)( selectedNode.getChildAt(0));
//   tn1.add(newChild);
       
//  }
       
       if (resultTree==null){
           resultTree = new JTree();
           getContentPane().add(new JScrollPane(resultTree), BorderLayout.CENTER);
           setVisible(true);
       }
       resultTree.setModel(new DefaultTreeModel(selectedNode));[/B]
 
 } 
  }
 
 private void processPrime(Element el, DefaultMutableTreeNode dmtn, int lev) {
     Iterator children = el.getChildren().iterator();
     while (children.hasNext()) {
 
 
      Object childObject = children.next();
         DefaultMutableTreeNode currentNode =  new DefaultMutableTreeNode(((Element) childObject).getName());
         processElement( (Element) childObject, dmtn);
 
       }    
     lev--;    
   }
 
 private void processTail(Element el, DefaultMutableTreeNode dmtn, int lev) {
 
      lev--;    
    }
 
    public static void main(String args[]) throws Exception {
      SAXBuilder builder = new SAXBuilder();
      Document doc = builder.build(new File("C://Users//Brugu Maharishi//Documents//Google Talk Received Files//cs3.html"));
      XMLTreeViewer viewer = new XMLTreeViewer(doc);
      viewer.addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(java.awt.event.WindowEvent e) {
          System.exit(0);
        }
      });
    }
  }

Thanks for replying to my queries.

I have one more question.
1. Is it possible for me to display only the tree structure without displaying the leaf nodes (I dont want to also display the root node)
2. I just want to get all the non-leaf nodes without the root node and display all non-leaf nodes(not needed in tree form)

Do we have any functions for retrieving the non-leaf nodes from the given tree?

Please help!!
Thanks!!

Do we have any functions for retrieving the non-leaf nodes from the given tree?

Well, the children() collection and isLeaf() method give you the means to retrieve those, but I'm not aware of a specific method that returns only the non-leaf descendants.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.