[jdom-interest] JDOM and JTree

Kenneth Ellefsen kellefsen at bergen.oilfield.slb.com
Mon Nov 25 04:39:24 PST 2002


Hi.

Here is something I made to do sort of what you are talking about. Using
recursion...I also made my own treenode (replaced with DefaultMutable in
excample) where I keep a referance of the Element to make updating
easyer.  Hope it's of use to you.

private DefaultMutableTreeNode addChildren(org.jdom.Element e){
      
   java.util.List l = e.getChildren();
   String s = e.getName();
   DefaultMutableTreeNode n = new DefaultMutableTreeNode(s);
   Iterator li = l.iterator();
   while(li.hasNext()){
      Element next = (Element)li.next();
      if(next.hasChildren())
         n.add( addChildren(next) );
   }
   return n;
}


On Mon, 2002-11-25 at 12:57, Xuemin Guan wrote:
> See the attachment, an example from "Java & XML". I used it for a demo
> purpose. It merely displays a XML document in a form of a visible tree.
> But should get you started.
> 
> Best,
> 
> Xuemin Guan
> 
> 时飞 wrote:
> > Hello Everyone,
> > 
> >      I am new to the list , but I'm currently coding a simple XML editor for specific internal 
> > reasons and I was wondering if anyone had done anything along the lines of displaying an
> > entire XML document in a java JTree object, and then being able to write
> > edits back to the file.  I'm having trouble combining the nodes on the tree
> > with the nodes of XML elements.  Any help would be Greatly appreciated.
> > 
> > best regards
> >  
> >              Luo Shifei          
> >    
> >         worldheart at 263.net
> >              
> >                                 2002-11-25
> > 
> > 
> > _______________________________________________
> > To control your jdom-interest membership:
> > http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com
> > 
> > 
> 
> 
> -- 
> Xuemin Guan
> Java & XML Developer
> Appresso SpA (www.appresso.com)
> Tel:  +81-3-4412-7790 (Direct)
>       +81-3-4412-7700 (Representative)
> Email: xuemin at appresso.com
> 
> ----
> 

> /**
>  * This class is used to display a XML document in a form of a
>  * interactive visible tree. When the window is closed, the system
>  * does not exit (it only release resource). If the client of this class
>  * wants to quit the whole system, the client program need either
>  * set the windowListener, or need to run System.exit(0) explictly.
>  */
> 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("XML");
> 		 initialize();
> 
> 	}
> 
> 	private void initialize(){
> 
> 		xmlTree = new JTree();
> 		xmlTree.setName("XML Tree");
> 		getContentPane().add(new JScrollPane(xmlTree), BorderLayout.CENTER);
> 
>         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);
> 	}
> 
> 
> 	 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
> 	{
> 		if(args.length != 1){
> 			System.out.println("Usage: java XMLTreeViewer "+
> 				"[XML Document filename]");
> 			return;
> 		}
> 
> 		SAXBuilder builder = new SAXBuilder();
> 		Document doc = builder.build(new File(args[0]));
> 		XMLTreeViewer viewer = new XMLTreeViewer(doc);
> 		viewer.addWindowListener(new java.awt.event.WindowAdapter(){
> 			      public void windowClosing(java.awt.event.WindowEvent e){
> 				  	System.exit(0);
> 				  }
> 			} );
> 	}
> }
> 
> 
> 
-- 
Mvh,

Kenneth Ellefsen                   | Tlf: (+47) 55985829
Schlumberger Information Solutions | www.sis.slb.com





More information about the jdom-interest mailing list