[jdom-interest] xml document parsar

Mattias Jiderhamn mj-lists at expertsystems.se
Thu May 18 08:31:46 PDT 2006


Try removing all the GUI/Swing stuff that has nothing to do with 
JDOM, and focus on a simplified example. Maybe you can figure it out 
yourself then (looking at JDOMs JavaDoc), or post the minimized 
example here for further help.

At 2006-05-18 16:51, cs_student wrote:
>Hi,
>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!
>
>this is my code
>package fina;
>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;
>import java.awt.event.*;
>import javax.swing.event.TreeSelectionEvent;
>import javax.swing.tree.DefaultMutableTreeNode;
>  public class XMLTreeViewer extends JFrame
>implements TreeSelectionListener {
>   //The JTree to display the XML
>private JTree xmlTree;
>//The XML document to be output to the JTree
>private Document xmlDoc;
>DefaultMutableTreeNode tn;
>   JButton AddButton = new JButton();
>   JTextField TextField = new JTextField();
>   JTextArea TextArea = new JTextArea();
>
>public XMLTreeViewer(Document doc) {
>   super();
>  // WindowUtilities.setNativeLookAndFeel();
>   addWindowListener(new ExitListener());
>   this.xmlDoc = doc;
>   setSize(600, 450);
>   tn = new DefaultMutableTreeNode("XML");
>   initialize();
>}
>private void initialize() {
>   xmlTree = new JTree();
>     xmlTree.setEditable(true);
>     AddButton.setBackground(SystemColor.inactiveCaptionText);
>     AddButton.setForeground(Color.black);
>     AddButton.setText("ADD");
>     AddButton.addMouseListener(new java.awt.event.MouseAdapter() {
>       public void mouseClicked(MouseEvent e) {
>         AddButton_mouseClicked(e);
>       }
>     });
>
>     TextField.setText("");
>     TextArea.setText("jTextArea1");
>     getContentPane().add(new JScrollPane(xmlTree), BorderLayout.CENTER);
>     this.getContentPane().add(TextField, BorderLayout.NORTH);
>     this.getContentPane().add(AddButton, BorderLayout.SOUTH);
>     this.getContentPane().add(TextArea, BorderLayout.EAST);
>
>     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);
>   //add Listener to Print to Screen the xml tag selected\
>   xmlTree.addTreeSelectionListener(this);
>}
>public void valueChanged(TreeSelectionEvent evt) {
>    // Get all nodes whose selection status has changed
>       TreePath[] paths = evt.getPaths();
>       // Print the last Path Component selected
>       System.out.println(evt.getPath().getLastPathComponent());
>//print the full path from the selected tag
>        System.out.println(evt.getPath().toString());
>    }
>
>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));
>   }
>   Iterator children = el.getChildren().iterator();
>   while (children.hasNext()) {
>     processElement( (Element) children.next(), currentNode);
>   }
>   dmtn.add(currentNode);
>}
>
>   public static void main(String[] args)throws Exception {
>     SAXBuilder builder = new SAXBuilder();
>     Document doc = builder.build("comp.xml");
>     XMLTreeViewer viewer = new XMLTreeViewer(doc);
>     viewer.addWindowListener(new java.awt.event.WindowAdapter() {
>       public void windowClosing(java.awt.event.WindowEvent e)
>       {System.exit(0);}
>    });
>   }
>    void AddButton_mouseClicked(MouseEvent e) {
>      TextArea.setSize(20,20);
>    TextArea.setText("An element will be\n added..");
>    TextField.getText();
>    Object o=xmlTree.getLastSelectedPathComponent();
>    addObject(o);
>   // String s=xmlTree.getLastSelectedPathComponent().toString();
>
>   }
>   public void addObject(Object child) {
>     DefaultMutableTreeNode parentNode=null;
>     TreePath parentPath=xmlTree.getSelectionPath();
>     if(parentPath!=null){
>       parentNode=(DefaultMutableTreeNode)(parentPath.getLastPathComponent());
>      addObject(parentNode, child, true);
>     }
>     }
>   public void addObject(DefaultMutableTreeNode parentNode, Object child,
>                         boolean b) {
>   }
>}
>//---------------------------------------
>package fina;
>import javax.swing.*;
>import java.awt.*;
>/** A few utilities that simplify using windows in Swing.
>  *  1998-99 Marty Hall, 
> <http://www.apl.jhu.edu/~hall/java/>http://www.apl.jhu.edu/~hall/java/
>  */
>public class WindowUtilities {
>   /** Tell system to use native look and feel, as in previous
>    *  releases. Metal (Java) LAF is the default otherwise.
>    */
>   public static void setNativeLookAndFeel() {
>     try {
>       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
>     } catch(Exception e) {
>       System.out.println("Error setting native LAF: " + e);
>     }
>   }
>   public static void setJavaLookAndFeel() {
>     try {
> 
>UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
>     } catch(Exception e) {
>       System.out.println("Error setting Java LAF: " + e);
>     }
>   }
>    public static void setMotifLookAndFeel() {
>     try {
> 
>UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
>     } catch(Exception e) {
>       System.out.println("Error setting Motif LAF: " + e);
>     }
>   }
>   /** A simplified way to see a JPanel or other Container.
>    *  Pops up a JFrame with specified Container as the content pane.
>    */
>   public static JFrame openInJFrame(Container content,
>                                     int width,
>                                     int height,
>                                     String title,
>                                     Color bgColor) {
>     JFrame frame = new JFrame(title);
>     frame.setBackground(bgColor);
>     content.setBackground(bgColor);
>     frame.setSize(width, height);
>     frame.setContentPane(content);
>     frame.addWindowListener(new ExitListener());
>     frame.setVisible(true);
>     return(frame);
>   }
>   /** Uses Color.white as the background color. */
>   public static JFrame openInJFrame(Container content,
>                                     int width,
>                                     int height,
>                                     String title) {
>     return(openInJFrame(content, width, height, title, Color.white));
>   }
>   /** Uses Color.white as the background color, and the
>    *  name of the Container's class as the JFrame title.
>    */
>   public static JFrame openInJFrame(Container content,
>                                     int width,
>                                     int height) {
>     return(openInJFrame(content, width, height,
>                         content.getClass().getName(),
>                         Color.white));
>   }
>}
>//----------------------------------------
>package fina;
>import java.awt.*;
>import java.awt.event.*;
>/** A listener that you attach to the top-level Frame or JFrame of
>  *  your application, so quitting the frame exits the application.
>  *  1998-99 Marty Hall, 
> <http://www.apl.jhu.edu/~hall/java/>http://www.apl.jhu.edu/~hall/java/
>  */
>public class ExitListener extends WindowAdapter {
>   public void windowClosing(WindowEvent event) {
>     System.exit(0);
>   }
>}
>thank you ..
>Bassam
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.jdom.org/pipermail/jdom-interest/attachments/20060518/86dd6102/attachment.htm


More information about the jdom-interest mailing list