[jdom-interest] xml document parsar

Edelson, Justin Justin.Edelson at mtvn.com
Thu May 18 08:20:10 PDT 2006


It's not clear to me where you want to add additional code.
 
To add an element to an existing parent element, you could do something
like this:
// assume element is an existing Element object
Element childElement = new Element("child");
element.addContent(childElement);
 
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:
element.removeChild("child");
 
If you want to remove all the children with a name:
element.removeChildren("child);
 
If you want to remove all children:
removeContent();
 
You could even use a ListIterator on element.getChildren() and call
ListIterator.remove()
 
Editing the name of an element is done by calling element.setName()
either with or without a Namespace
 
 
 

________________________________

From: jdom-interest-bounces at jdom.org
[mailto:jdom-interest-bounces at jdom.org] On Behalf Of cs_student
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.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/
 */
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/6042a690/attachment.htm


More information about the jdom-interest mailing list