flavour_of_bru 0 Newbie Poster

Hi,

I am working on the same part of the logic and got stuck here. The above code will work for linux, but I am working on windows platform.

I have seen in the post that you already gave such an example in the previous post, I searched for it but could not find it.

Can you please send me the link to that post or else if you can help me with the same in windows, that would be really great.

Thanks!!

flavour_of_bru 0 Newbie Poster

Hi,

I am presently trying to display an XML document as a tree structure.
I am able to do it using JTree and SAX Parser.

Now I would like to get the parents of all non-leaf nodes and display them as sub-trees.

Does we have any function in JTree where I can retrieve the parents of non-leaf nodes and display them??

Please help.

Thanks!!

flavour_of_bru 0 Newbie Poster

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!!

flavour_of_bru 0 Newbie Poster

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 …
flavour_of_bru 0 Newbie Poster
[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(){ …
flavour_of_bru 0 Newbie Poster

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!!!

flavour_of_bru 0 Newbie Poster
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 = …
flavour_of_bru 0 Newbie Poster

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!!!

flavour_of_bru 0 Newbie Poster

Hi,

I have a small question.
How do I compute the number of files(which are specific such as .doc) in a directory in C++.

For example, if I have 10 files of type .doc format in a directory, how do I compute this value using C++.

Any help is appreciated.

Thanks!!

flavour_of_bru 0 Newbie Poster

HI..
Thnx for the reply....

flavour_of_bru 0 Newbie Poster

Hi all,
Can you please tell me how to read .csv files in C++/VC++??
I converted my excel files to .csv files in order to make reading easy.

Need help regarding the code.

Thanks
Rishi.