[jdom-interest] JDOM and JTree
Xuemin Guan
xuemin at appresso.com
Mon Nov 25 03:57:52 PST 2002
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
-------------- next part --------------
/**
* 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);
}
} );
}
}
More information about the jdom-interest
mailing list