<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2900.2873" name=GENERATOR></HEAD>
<BODY>
<DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=578002015-18052006>I've 
got your example running in JBuilder now.</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN 
class=578002015-18052006></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=578002015-18052006>I 
don't have much experience with JTree so I can't really help you there but I 
*think* that what you need to do is add an element to the underlying XML 
document at the right spot, then re-build your visual tree.&nbsp; However, I'm 
not sure how you determine what the "right spot" is.&nbsp; Another option might 
be to only make changes to the visual tree, then on exit, use the visual tree 
structure&nbsp;to build an entirely new XML document and save it.&nbsp; It all 
depends on what you're trying to do.</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN 
class=578002015-18052006></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=578002015-18052006>The 
JDOM code to add an element&nbsp;looks something like 
this...</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN 
class=578002015-18052006></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN 
class=578002015-18052006>Element root = doc.getRootElement();<BR>Element e = new 
org.jdom.Element("tag_name_goes_here");<BR>root.addContent(e);</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN 
class=578002015-18052006></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=578002015-18052006>Note 
that you don't have to add just to the root, </SPAN></FONT><FONT face=Arial 
color=#0000ff size=2><SPAN class=578002015-18052006>addContent() can be applied 
to any element.</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN 
class=578002015-18052006></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN 
class=578002015-18052006></SPAN></FONT>&nbsp;</DIV></DIV>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
  <DIV class=OutlookMessageHeader dir=ltr align=left><FONT face=Tahoma 
  size=2>-----Original Message-----<BR><B>From:</B> cs_student 
  [mailto:cs_student@gawab.com]<BR><B>Sent:</B> Thursday, May 18, 2006 10:51 
  AM<BR><B>To:</B> jdom-interest@jdom.org<BR><B>Subject:</B> [jdom-interest] xml 
  document parsar <BR><BR></FONT></DIV>
  <DIV class=bgcolor>
  <DIV align=left>Hi,</DIV>
  <DIV align=left>I need for a help ..I don't know how to add an element to the 
  xml file,&nbsp; delete from the file or edite&nbsp;name of any element..I'm 
  using JBuilder..as you&nbsp;know that dealing with it&nbsp;is so hard!</DIV>
  <DIV align=left>&nbsp;</DIV>
  <DIV align=left>this is my code</DIV>
  <DIV align=left>package fina;<BR>import java.awt.*;<BR>import 
  javax.swing.*;<BR>import javax.swing.tree.*;<BR>import 
  javax.swing.event.*;<BR>import java.util.Iterator;<BR>import 
  java.util.List;<BR>import org.jdom.*;<BR>import 
  org.jdom.input.SAXBuilder;<BR>import java.io.File;<BR>import 
  java.awt.event.*;<BR>import javax.swing.event.TreeSelectionEvent;<BR>import 
  javax.swing.tree.DefaultMutableTreeNode;</DIV>
  <DIV align=left>&nbsp;public class XMLTreeViewer extends JFrame<BR>implements 
  TreeSelectionListener {</DIV>
  <DIV align=left>&nbsp; //The JTree to display the XML<BR>private JTree 
  xmlTree;</DIV>
  <DIV align=left>//The XML document to be output to the JTree<BR>private 
  Document xmlDoc;<BR>DefaultMutableTreeNode tn;</DIV>
  <DIV align=left>&nbsp; JButton AddButton = new JButton();<BR>&nbsp; JTextField 
  TextField = new JTextField();<BR>&nbsp; JTextArea TextArea = new 
  JTextArea();</DIV>
  <DIV align=left>&nbsp;</DIV>
  <DIV align=left>public XMLTreeViewer(Document doc) {<BR>&nbsp; 
  super();<BR>&nbsp;// WindowUtilities.setNativeLookAndFeel();<BR>&nbsp; 
  addWindowListener(new ExitListener());<BR>&nbsp; this.xmlDoc = doc;<BR>&nbsp; 
  setSize(600, 450);<BR>&nbsp; tn = new DefaultMutableTreeNode("XML");<BR>&nbsp; 
  initialize();<BR>}</DIV>
  <DIV align=left>private void initialize() {<BR>&nbsp; xmlTree = new 
  JTree();<BR>&nbsp;&nbsp;&nbsp; 
  xmlTree.setEditable(true);<BR>&nbsp;&nbsp;&nbsp; 
  AddButton.setBackground(SystemColor.inactiveCaptionText);<BR>&nbsp;&nbsp;&nbsp; 
  AddButton.setForeground(Color.black);<BR>&nbsp;&nbsp;&nbsp; 
  AddButton.setText("ADD");<BR>&nbsp;&nbsp;&nbsp; AddButton.addMouseListener(new 
  java.awt.event.MouseAdapter() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void 
  mouseClicked(MouseEvent e) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  AddButton_mouseClicked(e);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  }<BR>&nbsp;&nbsp;&nbsp; });</DIV>
  <DIV align=left><BR>&nbsp;&nbsp;&nbsp; 
  TextField.setText("");<BR>&nbsp;&nbsp;&nbsp; 
  TextArea.setText("jTextArea1");<BR>&nbsp;&nbsp;&nbsp; getContentPane().add(new 
  JScrollPane(xmlTree), BorderLayout.CENTER);<BR>&nbsp;&nbsp;&nbsp; 
  this.getContentPane().add(TextField, 
  BorderLayout.NORTH);<BR>&nbsp;&nbsp;&nbsp; 
  this.getContentPane().add(AddButton, 
  BorderLayout.SOUTH);<BR>&nbsp;&nbsp;&nbsp; this.getContentPane().add(TextArea, 
  BorderLayout.EAST);</DIV>
  <DIV align=left>&nbsp;</DIV>
  <DIV align=left>&nbsp;&nbsp;&nbsp; processElement(xmlDoc.getRootElement(), 
  tn);<BR>&nbsp; ( (DefaultTreeModel) xmlTree.getModel()).setRoot(tn);<BR>&nbsp; 
  addWindowListener(new java.awt.event.WindowAdapter() {<BR>&nbsp;&nbsp;&nbsp; 
  public void windowClosing(java.awt.event.WindowEvent e) 
  {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //release all the 
  resource<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xmlTree = 
  null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tn = null;<BR>&nbsp;&nbsp;&nbsp; 
  }<BR>&nbsp; });<BR>&nbsp; setVisible(true);</DIV>
  <DIV align=left>&nbsp; //add Listener to Print to Screen the xml tag 
  selected\<BR>&nbsp; xmlTree.addTreeSelectionListener(this);<BR>}</DIV>
  <DIV align=left>public void valueChanged(TreeSelectionEvent evt) {</DIV>
  <DIV align=left>&nbsp;&nbsp; // Get all nodes whose selection status has 
  changed<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TreePath[] paths = 
  evt.getPaths();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Print the last Path 
  Component selected<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  System.out.println(evt.getPath().getLastPathComponent());</DIV>
  <DIV align=left>//print the full path from the selected 
  tag<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  System.out.println(evt.getPath().toString());<BR>&nbsp;&nbsp; }</DIV>
  <DIV align=left><BR>private void processElement(Element el, 
  DefaultMutableTreeNode dmtn) {<BR>&nbsp; DefaultMutableTreeNode currentNode 
  =<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new 
  DefaultMutableTreeNode(el.getName());<BR>&nbsp; String text = 
  el.getTextNormalize();<BR>&nbsp; if ( (text != null) &amp;&amp; 
  (!text.equals(""))) {<BR>&nbsp;&nbsp;&nbsp; currentNode.add(new 
  DefaultMutableTreeNode(text));<BR>&nbsp; }</DIV>
  <DIV align=left>&nbsp; Iterator children = 
  el.getChildren().iterator();<BR>&nbsp; while (children.hasNext()) 
  {<BR>&nbsp;&nbsp;&nbsp; processElement( (Element) children.next(), 
  currentNode);<BR>&nbsp; }<BR>&nbsp; dmtn.add(currentNode);<BR>}</DIV>
  <DIV align=left><BR>&nbsp; public static void main(String[] args)throws 
  Exception {</DIV>
  <DIV align=left>&nbsp;&nbsp;&nbsp; SAXBuilder builder = new 
  SAXBuilder();<BR>&nbsp;&nbsp;&nbsp; Document doc = 
  builder.build("comp.xml");<BR>&nbsp;&nbsp;&nbsp; XMLTreeViewer viewer = new 
  XMLTreeViewer(doc);<BR>&nbsp;&nbsp;&nbsp; viewer.addWindowListener(new 
  java.awt.event.WindowAdapter() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void 
  windowClosing(java.awt.event.WindowEvent e)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  {System.exit(0);}<BR>&nbsp;&nbsp; });</DIV>
  <DIV align=left>&nbsp; }</DIV>
  <DIV align=left>&nbsp;&nbsp; void AddButton_mouseClicked(MouseEvent e) 
  {<BR>&nbsp;&nbsp;&nbsp;&nbsp; TextArea.setSize(20,20);<BR>&nbsp;&nbsp; 
  TextArea.setText("An element will be\n added..");<BR>&nbsp;&nbsp; 
  TextField.getText();<BR>&nbsp;&nbsp; Object 
  o=xmlTree.getLastSelectedPathComponent();<BR>&nbsp;&nbsp; 
  addObject(o);<BR>&nbsp; // String 
  s=xmlTree.getLastSelectedPathComponent().toString();</DIV>
  <DIV align=left><BR>&nbsp; }</DIV>
  <DIV align=left>&nbsp; public void addObject(Object child) 
  {<BR>&nbsp;&nbsp;&nbsp; DefaultMutableTreeNode 
  parentNode=null;<BR>&nbsp;&nbsp;&nbsp; TreePath 
  parentPath=xmlTree.getSelectionPath();<BR>&nbsp;&nbsp;&nbsp; 
  if(parentPath!=null){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  parentNode=(DefaultMutableTreeNode)(parentPath.getLastPathComponent());<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
  addObject(parentNode, child, true);<BR>&nbsp;&nbsp;&nbsp; 
  }<BR>&nbsp;&nbsp;&nbsp; }</DIV>
  <DIV align=left>&nbsp; public void addObject(DefaultMutableTreeNode 
  parentNode, Object 
  child,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  boolean b) {<BR>&nbsp; }<BR>}<BR></DIV>
  <DIV align=left>//---------------------------------------</DIV>
  <DIV align=left>package fina;<BR>import javax.swing.*;<BR>import 
  java.awt.*;</DIV>
  <DIV align=left>/** A few utilities that simplify using windows in 
  Swing.<BR>&nbsp;*&nbsp; 1998-99 Marty Hall, <A 
  href="http://www.apl.jhu.edu/~hall/java/">http://www.apl.jhu.edu/~hall/java/</A><BR>&nbsp;*/</DIV>
  <DIV align=left>public class WindowUtilities {</DIV>
  <DIV align=left>&nbsp; /** Tell system to use native look and feel, as in 
  previous<BR>&nbsp;&nbsp; *&nbsp; releases. Metal (Java) LAF is the default 
  otherwise.<BR>&nbsp;&nbsp; */</DIV>
  <DIV align=left>&nbsp; public static void setNativeLookAndFeel() 
  {<BR>&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());<BR>&nbsp;&nbsp;&nbsp; 
  } catch(Exception e) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  System.out.println("Error setting native LAF: " + e);<BR>&nbsp;&nbsp;&nbsp; 
  }<BR>&nbsp; }</DIV>
  <DIV align=left>&nbsp; public static void setJavaLookAndFeel() 
  {<BR>&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());<BR>&nbsp;&nbsp;&nbsp; 
  } catch(Exception e) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  System.out.println("Error setting Java LAF: " + e);<BR>&nbsp;&nbsp;&nbsp; 
  }<BR>&nbsp; }</DIV>
  <DIV align=left>&nbsp;&nbsp; public static void setMotifLookAndFeel() 
  {<BR>&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");<BR>&nbsp;&nbsp;&nbsp; 
  } catch(Exception e) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  System.out.println("Error setting Motif LAF: " + e);<BR>&nbsp;&nbsp;&nbsp; 
  }<BR>&nbsp; }</DIV>
  <DIV align=left>&nbsp; /** A simplified way to see a JPanel or other 
  Container.<BR>&nbsp;&nbsp; *&nbsp; Pops up a JFrame with specified Container 
  as the content pane.<BR>&nbsp;&nbsp; */</DIV>
  <DIV align=left>&nbsp; public static JFrame openInJFrame(Container 
  content,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  int 
  width,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  int 
  height,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  String 
  title,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  Color bgColor) {<BR>&nbsp;&nbsp;&nbsp; JFrame frame = new 
  JFrame(title);<BR>&nbsp;&nbsp;&nbsp; 
  frame.setBackground(bgColor);<BR>&nbsp;&nbsp;&nbsp; 
  content.setBackground(bgColor);<BR>&nbsp;&nbsp;&nbsp; frame.setSize(width, 
  height);<BR>&nbsp;&nbsp;&nbsp; 
  frame.setContentPane(content);<BR>&nbsp;&nbsp;&nbsp; 
  frame.addWindowListener(new ExitListener());<BR>&nbsp;&nbsp;&nbsp; 
  frame.setVisible(true);<BR>&nbsp;&nbsp;&nbsp; return(frame);<BR>&nbsp; }</DIV>
  <DIV align=left>&nbsp; /** Uses Color.white as the background color. */</DIV>
  <DIV align=left>&nbsp; public static JFrame openInJFrame(Container 
  content,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  int 
  width,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  int 
  height,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  String title) {<BR>&nbsp;&nbsp;&nbsp; return(openInJFrame(content, width, 
  height, title, Color.white));<BR>&nbsp; }</DIV>
  <DIV align=left>&nbsp; /** Uses Color.white as the background color, and 
  the<BR>&nbsp;&nbsp; *&nbsp; name of the Container's class as the JFrame 
  title.<BR>&nbsp;&nbsp; */</DIV>
  <DIV align=left>&nbsp; public static JFrame openInJFrame(Container 
  content,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  int 
  width,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  int height) {<BR>&nbsp;&nbsp;&nbsp; return(openInJFrame(content, width, 
  height,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  content.getClass().getName(),<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  Color.white));<BR>&nbsp; 
  }<BR>}<BR>//----------------------------------------</DIV>
  <DIV align=left>package fina;<BR>import java.awt.*;<BR>import 
  java.awt.event.*;</DIV>
  <DIV align=left>/** A listener that you attach to the top-level Frame or 
  JFrame of<BR>&nbsp;*&nbsp; your application, so quitting the frame exits the 
  application.<BR>&nbsp;*&nbsp; 1998-99 Marty Hall, <A 
  href="http://www.apl.jhu.edu/~hall/java/">http://www.apl.jhu.edu/~hall/java/</A><BR>&nbsp;*/</DIV>
  <DIV align=left>public class ExitListener extends WindowAdapter {<BR>&nbsp; 
  public void windowClosing(WindowEvent event) {<BR>&nbsp;&nbsp;&nbsp; 
  System.exit(0);<BR>&nbsp; }<BR>}<BR></DIV>
  <DIV align=left>thank you ..</DIV>
  <DIV align=left>Bassam</DIV>
  <DIV align=left>&nbsp;</DIV></DIV>
  <DIV>________________________________</DIV>
  <DIV>Free POP3 Email from <A href="http://www.gawab.com" 
  target=_blank>www.gawab.com</A></DIV>
  <DIV>Sign up NOW and get your account 
@gawab.com!!</DIV></BLOCKQUOTE></BODY></HTML>