473,804 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.tre e.*;
import javax.swing.eve nt.*;
import javax.xml.parse rs.*;
import org.w3c.dom.*;

public class XmlTreeDemo extends JFrame {
XmlTreeDemo(Str ing title){
super(title);
try{
DocumentBuilder Factory IDocumentBuilde rFactory
= DocumentBuilder Factory.newInst ance();
DocumentBuilder IDocumentBuilde r
= IDocumentBuilde rFactory.newDoc umentBuilder();
Document IDocument = IDocumentBuilde r.parse("c:/test1.xml");
Node root = IDocument.getDo cumentElement() ;
XmlTreeModel model = new XmlTreeModel(ro ot);
JTree IJTree = new JTree();
IJTree.setModel (model);
getContentPane( ).add(new JScrollPane(IJT ree),BorderLayo ut.CENTER);
setDefaultClose Operation(JFram e.EXIT_ON_CLOSE );
}
catch (Exception e){
System.err.prin tln(e);
}
}
public static void main(String[] args){
XmlTreeDemo IJTreeDemo = new XmlTreeDemo("Xm l tree demo");
IJTreeDemo.pack ();
IJTreeDemo.show ();
}
}
class XmlTreeModel implements TreeModel{
protected Node root;
public XmlTreeModel(No de root){
this.root = root;
}
public Object getRoot(){
return (Object)this.ro ot;
}
public boolean isLeaf(Object node){
if ((((Node)node). getNodeType() == 7) || (((Node)node).g etNodeType()
== 1)) return false;
return true;
}
public int getChildCount(O bject 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.getLeng th() == 0) return -1;
for (int i=0; i<childs.getLen gth(); i++){
if (childs.item(i) == (Node)child) return i;
}
return -1;
}
public void valueForPathCha nged(TreePath path, Object newValue){
}
public void addTreeModelLis tener(TreeModel Listener l){
}
public void removeTreeModel Listener(TreeMo delListener l){
}
}
Jul 17 '05 #1
2 11131
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 TreeCellRendere r to display
whatever you like.

Ray

--
XML is the programmer's duct tape.
Jul 17 '05 #2
Raymond DeCampo <rd******@spam. twcny.spam.rr.s pam.com.spam> wrote in message news:<kK******* ************@tw ister.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
5916
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 it would do so. Sample code is : JPanel logoPanel = new JPanel(); logoPanel.setLayout(new BoxLayout(logoPanel, BoxLayout.X_AXIS)); ImageIcon logo = new ImageIcon(_imageFileName);
1
2314
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 all Bernard
0
1693
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 JTree and how to edit them. Do you have any advice? Thanks
1
2023
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"> <loc>Newport</loc>
13
1982
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 hidden input control. How to do that? I am getting the error in the following case. <TR id="CCRow" style="DISPLAY:"
2
4498
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 display this information in 5 different label controls in an ASP.NET web page. When I test the stored procedure in a GridView all the columns display correctly (I am not manually defining the columns). When I try to use labels I can display all...
1
2625
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. Included in the application program is a data “GRID” which I believe was created using Visual Basic, not standard Access. Its purpose is to display the results of a range of “from-to-dates” of registrations for different clients. The difficulty...
11
8722
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 found is to write it to a temp file and then use the Navigate method to load it. As the control is capable of loading it from disk, I would like to find a way to skip the save/load and just inject. Thanks, Dave
21
3175
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 website through the database. you can see what i have done for now at this url http://www.scale24-25.com/kitDB-site.php this is the code
0
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10354
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10359
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10101
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9177
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.