<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2900.2873" name=GENERATOR></HEAD>
<BODY>
<DIV dir=ltr align=left><SPAN class=712041515-18052006><FONT face=Arial
color=#0000ff size=2>It's not clear to me where you want to add additional
code.</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=712041515-18052006><FONT face=Arial
color=#0000ff size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=712041515-18052006><FONT face=Arial
color=#0000ff size=2>To add an element to an existing parent element, you could
do something like this:</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=712041515-18052006><FONT face=Arial
color=#0000ff size=2>// assume element is an existing Element
object</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=712041515-18052006><FONT face=Arial
color=#0000ff size=2>Element childElement = new
Element("child");</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=712041515-18052006><FONT face=Arial
color=#0000ff size=2>element.addContent(childElement);</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=712041515-18052006><FONT face=Arial
color=#0000ff size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=712041515-18052006><FONT face=Arial
color=#0000ff size=2>To delete an element from an existing parent element, you
have a few options. If you know there's only one child element with a specific
name, you could do:</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=712041515-18052006><FONT face=Arial
color=#0000ff size=2>element.removeChild("child");</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=712041515-18052006><FONT face=Arial
color=#0000ff size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=712041515-18052006><FONT face=Arial
color=#0000ff size=2>If you want to remove all the children with a
name:</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=712041515-18052006><FONT face=Arial
color=#0000ff size=2>element.removeChildren("child);</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=712041515-18052006><FONT face=Arial
color=#0000ff size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=712041515-18052006><FONT face=Arial
color=#0000ff size=2>If you want to remove all children:</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=712041515-18052006><FONT face=Arial
color=#0000ff size=2>removeContent();</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=712041515-18052006><FONT face=Arial
color=#0000ff size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=712041515-18052006><FONT face=Arial
color=#0000ff size=2>You could even use a ListIterator on element.getChildren()
and call ListIterator.remove()</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=712041515-18052006><FONT face=Arial
color=#0000ff size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=712041515-18052006><FONT face=Arial
color=#0000ff size=2>Editing the name of an element is done by calling
element.setName() either with or without a Namespace</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=712041515-18052006><FONT face=Arial
color=#0000ff size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=712041515-18052006></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=712041515-18052006><FONT face=Arial
color=#0000ff size=2></FONT></SPAN> </DIV><BR>
<DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left>
<HR tabIndex=-1>
<FONT face=Tahoma size=2><B>From:</B> jdom-interest-bounces@jdom.org
[mailto:jdom-interest-bounces@jdom.org] <B>On Behalf Of
</B>cs_student<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></FONT><BR></DIV>
<DIV></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></BODY></HTML>