473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Display xml file in JTree: Select information to be shown in node label

Hi there,
I have managed to write a small java program that display an xml file
in a JTree. This was achieved by defining a suitable tree model. THe
program works...somehow. The nodes (elements and texts) are correctly
recognized, and the corresponding branches in the JTree can be opened
and closed. However, when I run the program, all the non-leaf nodes
(e. g. the element nodes) are displayed from the start tag to the end
tag including the tag delimiters. Is there any way to control what is
used as a "node label" in a JTree? Of course that could be done by
brute (e.g. by building the JTree from simple strings) but I believe
that this approach will destroy the tree structure of the underlying
xml file.
I have tried for some time to display xml data in a tree view, and a
java solution seemed to be the most "organic" one. So it would be
great if that program to be really made to work properly. Many thanks
for any idea how to achieve this! Piet
Here is the code:
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

public class XmlTreeDemo extends JFrame {
XmlTreeDemo(String title){
super(title);
try{
DocumentBuilderFactory IDocumentBuilderFactory
= DocumentBuilderFactory.newInstance();
DocumentBuilder IDocumentBuilder
= IDocumentBuilderFactory.newDocumentBuilder();
Document IDocument = IDocumentBuilder.parse("c:/test1.xml");
Node root = IDocument.getDocumentElement();
XmlTreeModel model = new XmlTreeModel(root);
JTree IJTree = new JTree();
IJTree.setModel(model);
getContentPane().add(new JScrollPane(IJTree),BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
catch (Exception e){
System.err.println(e);
}
}
public static void main(String[] args){
XmlTreeDemo IJTreeDemo = new XmlTreeDemo("Xml tree demo");
IJTreeDemo.pack();
IJTreeDemo.show();
}
}
class XmlTreeModel implements TreeModel{
protected Node root;
public XmlTreeModel(Node root){
this.root = root;
}
public Object getRoot(){
return (Object)this.root;
}
public boolean isLeaf(Object node){
if ((((Node)node).getNodeType() == 7) || (((Node)node).getNodeType()
== 1)) return false;
return true;
}
public int getChildCount(Object parent){
return ((Node)parent).getChildNodes().getLength();
}
public Object getChild(Object parent,int index){
Node child = ((Node)parent).getChildNodes().item(index);
return (Object)child;
}
public int getIndexOfChild(Object parent, Object child){
NodeList childs = ((Node)parent).getChildNodes();
if (childs.getLength() == 0) return -1;
for (int i=0; i<childs.getLength(); i++){
if (childs.item(i) == (Node)child) return i;
}
return -1;
}
public void valueForPathChanged(TreePath path, Object newValue){
}
public void addTreeModelListener(TreeModelListener l){
}
public void removeTreeModelListener(TreeModelListener l){
}
}
Jul 17 '05 #1
2 11096
Piet wrote:
Hi there,
I have managed to write a small java program that display an xml file
in a JTree. This was achieved by defining a suitable tree model. THe
program works...somehow. The nodes (elements and texts) are correctly
recognized, and the corresponding branches in the JTree can be opened
and closed. However, when I run the program, all the non-leaf nodes
(e. g. the element nodes) are displayed from the start tag to the end
tag including the tag delimiters. Is there any way to control what is
used as a "node label" in a JTree? Of course that could be done by
brute (e.g. by building the JTree from simple strings) but I believe
that this approach will destroy the tree structure of the underlying
xml file.


The default rendering will call toString() on the object associated to
the node. So, if appropriate, you could override toString() to get what
you want.

Of course, that is the easy way and is frequently not possible. In such
a case, you can implement and set a custom TreeCellRenderer to display
whatever you like.

Ray

--
XML is the programmer's duct tape.
Jul 17 '05 #2
Raymond DeCampo <rd******@spam.twcny.spam.rr.spam.com.spam> wrote in message news:<kK*******************@twister.nyroc.rr.com>. ..
Hi Raymond,
many thanks for the hint! In fact it was only 10 lines of code to improve it.
Piet
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: prabhat | last post by:
Hi, I am getting this weird behavior. I have couple of gif and jpg files that I would like to display in JLabel. It displays one jpg image but it does not display other gifs and jpgs. Any idea why...
1
by: Bernard Koninckx | last post by:
Hello everybody, I'vee a small problem with a JTree. I make an expand from a node. And after this operation the selection is lost. How can I do reselect the node than I come expand ? Thanks...
0
by: Lucia | last post by:
hello, I habe a Java Program that displays the XML document in a Java JTree using DOM. Now I can add, delete and edit the JTree Elements. But I don't know how to show the attributes also in the...
1
by: Charlie T | last post by:
hello, I need a little guidance here... I am tring to parse out an XML file, but with some restrictions. here is my XML FILE: ---------XML----------- <XML> <Cam name="01">...
13
by: Benjamin Smith | last post by:
I am controlling the display status of a table row using the following code. <TR id="CCRow" style="DISPLAY:none"> Instead of hard coding "none" above, I would like to change that value using a...
2
by: Joe Campbell | last post by:
I have a DBA that wrote a stored procedure that does a SELECT from a particluar SQL Server table. Within that stored procedure he links over to grab a column from another database table. I need to...
1
by: John Phelan-Cummings | last post by:
When I add the name of a new individual in a, bound form, it will not display that person’s name in a label control of a second unbound form. I have a scheduling program that I am working on. ...
11
by: Webbert | last post by:
I am trying to display XML in a WebBrowser Control. I receive a data feed of XML and am trying to inject it into the control. I have not been successful in doing so. The only solution I have...
21
by: scaleautostyle | last post by:
hi everybody... i ran across the site and found TONS of information about what i want to do.... but i don't know what to take for my need. here is what i want to do... a search engine for my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.