[jdom-interest] xml document parsar

William Krick wkrick at eio-online.com
Thu May 18 08:33:31 PDT 2006


I've got your example running in JBuilder now.

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.

The JDOM code to add an element looks something like this...

Element root = doc.getRootElement();
Element e = new org.jdom.Element("tag_name_goes_here");
root.addContent(e);

Note that you don't have to add just to the root, addContent() can be
applied to any element.


  -----Original Message-----
  From: cs_student [mailto:cs_student at gawab.com]
  Sent: Thursday, May 18, 2006 10:51 AM
  To: jdom-interest at jdom.org
  Subject: [jdom-interest] xml document parsar


  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/
   */
  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.getCrossPlatformLookAndFeelClassN
ame());
      } catch(Exception e) {
        System.out.println("Error setting Java LAF: " + e);
      }
    }
     public static void setMotifLookAndFeel() {
      try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAnd
Feel");
      } 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/
   */
  public class ExitListener extends WindowAdapter {
    public void windowClosing(WindowEvent event) {
      System.exit(0);
    }
  }

  thank you ..
  Bassam

  ________________________________
  Free POP3 Email from www.gawab.com
  Sign up NOW and get your account @gawab.com!!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.jdom.org/pipermail/jdom-interest/attachments/20060518/458fef6c/attachment.htm


More information about the jdom-interest mailing list