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){
}
}
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){
}
}
Comment