<!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> </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. However, I'm
not sure how you determine what the "right spot" is. Another option might
be to only make changes to the visual tree, then on exit, use the visual tree
structure to build an entirely new XML document and save it. 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> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=578002015-18052006>The
JDOM code to add an element looks something like
this...</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=578002015-18052006></SPAN></FONT> </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> </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> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=578002015-18052006></SPAN></FONT> </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, delete from the file or edite name of any element..I'm
using JBuilder..as you know that dealing with it is so hard!</DIV>
<DIV align=left> </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> public class XMLTreeViewer extends JFrame<BR>implements
TreeSelectionListener {</DIV>
<DIV align=left> //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> JButton AddButton = new JButton();<BR> JTextField
TextField = new JTextField();<BR> JTextArea TextArea = new
JTextArea();</DIV>
<DIV align=left> </DIV>
<DIV align=left>public XMLTreeViewer(Document doc) {<BR>
super();<BR> // WindowUtilities.setNativeLookAndFeel();<BR>
addWindowListener(new ExitListener());<BR> this.xmlDoc = doc;<BR>
setSize(600, 450);<BR> tn = new DefaultMutableTreeNode("XML");<BR>
initialize();<BR>}</DIV>
<DIV align=left>private void initialize() {<BR> xmlTree = new
JTree();<BR>
xmlTree.setEditable(true);<BR>
AddButton.setBackground(SystemColor.inactiveCaptionText);<BR>
AddButton.setForeground(Color.black);<BR>
AddButton.setText("ADD");<BR> AddButton.addMouseListener(new
java.awt.event.MouseAdapter() {<BR> public void
mouseClicked(MouseEvent e) {<BR>
AddButton_mouseClicked(e);<BR>
}<BR> });</DIV>
<DIV align=left><BR>
TextField.setText("");<BR>
TextArea.setText("jTextArea1");<BR> getContentPane().add(new
JScrollPane(xmlTree), BorderLayout.CENTER);<BR>
this.getContentPane().add(TextField,
BorderLayout.NORTH);<BR>
this.getContentPane().add(AddButton,
BorderLayout.SOUTH);<BR> this.getContentPane().add(TextArea,
BorderLayout.EAST);</DIV>
<DIV align=left> </DIV>
<DIV align=left> processElement(xmlDoc.getRootElement(),
tn);<BR> ( (DefaultTreeModel) xmlTree.getModel()).setRoot(tn);<BR>
addWindowListener(new java.awt.event.WindowAdapter() {<BR>
public void windowClosing(java.awt.event.WindowEvent e)
{<BR> //release all the
resource<BR> xmlTree =
null;<BR> tn = null;<BR>
}<BR> });<BR> setVisible(true);</DIV>
<DIV align=left> //add Listener to Print to Screen the xml tag
selected\<BR> xmlTree.addTreeSelectionListener(this);<BR>}</DIV>
<DIV align=left>public void valueChanged(TreeSelectionEvent evt) {</DIV>
<DIV align=left> // Get all nodes whose selection status has
changed<BR> TreePath[] paths =
evt.getPaths();<BR> // Print the last Path
Component selected<BR>
System.out.println(evt.getPath().getLastPathComponent());</DIV>
<DIV align=left>//print the full path from the selected
tag<BR>
System.out.println(evt.getPath().toString());<BR> }</DIV>
<DIV align=left><BR>private void processElement(Element el,
DefaultMutableTreeNode dmtn) {<BR> DefaultMutableTreeNode currentNode
=<BR> new
DefaultMutableTreeNode(el.getName());<BR> String text =
el.getTextNormalize();<BR> if ( (text != null) &&
(!text.equals(""))) {<BR> currentNode.add(new
DefaultMutableTreeNode(text));<BR> }</DIV>
<DIV align=left> Iterator children =
el.getChildren().iterator();<BR> while (children.hasNext())
{<BR> processElement( (Element) children.next(),
currentNode);<BR> }<BR> dmtn.add(currentNode);<BR>}</DIV>
<DIV align=left><BR> public static void main(String[] args)throws
Exception {</DIV>
<DIV align=left> SAXBuilder builder = new
SAXBuilder();<BR> Document doc =
builder.build("comp.xml");<BR> XMLTreeViewer viewer = new
XMLTreeViewer(doc);<BR> viewer.addWindowListener(new
java.awt.event.WindowAdapter() {<BR> public void
windowClosing(java.awt.event.WindowEvent e)<BR>
{System.exit(0);}<BR> });</DIV>
<DIV align=left> }</DIV>
<DIV align=left> void AddButton_mouseClicked(MouseEvent e)
{<BR> TextArea.setSize(20,20);<BR>
TextArea.setText("An element will be\n added..");<BR>
TextField.getText();<BR> Object
o=xmlTree.getLastSelectedPathComponent();<BR>
addObject(o);<BR> // String
s=xmlTree.getLastSelectedPathComponent().toString();</DIV>
<DIV align=left><BR> }</DIV>
<DIV align=left> public void addObject(Object child)
{<BR> DefaultMutableTreeNode
parentNode=null;<BR> TreePath
parentPath=xmlTree.getSelectionPath();<BR>
if(parentPath!=null){<BR>
parentNode=(DefaultMutableTreeNode)(parentPath.getLastPathComponent());<BR>
addObject(parentNode, child, true);<BR>
}<BR> }</DIV>
<DIV align=left> public void addObject(DefaultMutableTreeNode
parentNode, Object
child,<BR>
boolean b) {<BR> }<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> * 1998-99 Marty Hall, <A
href="http://www.apl.jhu.edu/~hall/java/">http://www.apl.jhu.edu/~hall/java/</A><BR> */</DIV>
<DIV align=left>public class WindowUtilities {</DIV>
<DIV align=left> /** Tell system to use native look and feel, as in
previous<BR> * releases. Metal (Java) LAF is the default
otherwise.<BR> */</DIV>
<DIV align=left> public static void setNativeLookAndFeel()
{<BR> try {<BR>
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());<BR>
} catch(Exception e) {<BR>
System.out.println("Error setting native LAF: " + e);<BR>
}<BR> }</DIV>
<DIV align=left> public static void setJavaLookAndFeel()
{<BR> try {<BR>
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());<BR>
} catch(Exception e) {<BR>
System.out.println("Error setting Java LAF: " + e);<BR>
}<BR> }</DIV>
<DIV align=left> public static void setMotifLookAndFeel()
{<BR> try {<BR>
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");<BR>
} catch(Exception e) {<BR>
System.out.println("Error setting Motif LAF: " + e);<BR>
}<BR> }</DIV>
<DIV align=left> /** A simplified way to see a JPanel or other
Container.<BR> * Pops up a JFrame with specified Container
as the content pane.<BR> */</DIV>
<DIV align=left> public static JFrame openInJFrame(Container
content,<BR>
int
width,<BR>
int
height,<BR>
String
title,<BR>
Color bgColor) {<BR> JFrame frame = new
JFrame(title);<BR>
frame.setBackground(bgColor);<BR>
content.setBackground(bgColor);<BR> frame.setSize(width,
height);<BR>
frame.setContentPane(content);<BR>
frame.addWindowListener(new ExitListener());<BR>
frame.setVisible(true);<BR> return(frame);<BR> }</DIV>
<DIV align=left> /** Uses Color.white as the background color. */</DIV>
<DIV align=left> public static JFrame openInJFrame(Container
content,<BR>
int
width,<BR>
int
height,<BR>
String title) {<BR> return(openInJFrame(content, width,
height, title, Color.white));<BR> }</DIV>
<DIV align=left> /** Uses Color.white as the background color, and
the<BR> * name of the Container's class as the JFrame
title.<BR> */</DIV>
<DIV align=left> public static JFrame openInJFrame(Container
content,<BR>
int
width,<BR>
int height) {<BR> return(openInJFrame(content, width,
height,<BR>
content.getClass().getName(),<BR>
Color.white));<BR>
}<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> * your application, so quitting the frame exits the
application.<BR> * 1998-99 Marty Hall, <A
href="http://www.apl.jhu.edu/~hall/java/">http://www.apl.jhu.edu/~hall/java/</A><BR> */</DIV>
<DIV align=left>public class ExitListener extends WindowAdapter {<BR>
public void windowClosing(WindowEvent event) {<BR>
System.exit(0);<BR> }<BR>}<BR></DIV>
<DIV align=left>thank you ..</DIV>
<DIV align=left>Bassam</DIV>
<DIV align=left> </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>