JDOM将内容写入现有文件而不删除

时间:2013-11-27 17:24:17

标签: java xml database jframe jdom

我在xml中创建了一个数据库,我想在Jframe中创建一个寄存器表单来向db添加对象。所以我建立了代码,但每次单击Registra按钮时,它都会覆盖我的文件。如何保留文件并简单地添加元素而不删除其他元素?这是代码:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.*;
import java.text.Format;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;


public class Registrati extends JFrame {

    private JPanel contentPane;
    private static JTextField textField;
    private static JTextField textField_1;
    private static JTextField textField_2;
    private static JTextField textField_3;
    private static JTextField textField_4;
    private static JTextField textField_5;
    private static JTextField textField_6;
    private static JPasswordField passwordField;
    private static JTextField textField_7;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Registrati frame = new Registrati();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Registrati() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 463, 460);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblInserisciITuoi = new JLabel("Inserisci i tuoi dati per registrarti");
        lblInserisciITuoi.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblInserisciITuoi.setBounds(156, 23, 215, 14);
        contentPane.add(lblInserisciITuoi);

        JLabel lblNome = new JLabel("Nome");
        lblNome.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblNome.setBounds(38, 58, 46, 14);
        contentPane.add(lblNome);

        textField = new JTextField();
        textField.setBounds(127, 56, 229, 20);
        contentPane.add(textField);
        textField.setColumns(10);

        JLabel lblCognome = new JLabel("Cognome");
        lblCognome.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblCognome.setBounds(38, 89, 64, 14);
        contentPane.add(lblCognome);

        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(127, 87, 229, 20);
        contentPane.add(textField_1);

        JLabel lblDataDiNascita = new JLabel("Data di Nascita");
        lblDataDiNascita.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblDataDiNascita.setBounds(38, 120, 98, 14);
        contentPane.add(lblDataDiNascita);

        textField_2 = new JTextField();
        textField_2.setColumns(10);
        textField_2.setBounds(127, 118, 229, 20);
        contentPane.add(textField_2);

        JLabel lblAltezza = new JLabel("Altezza");
        lblAltezza.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblAltezza.setBounds(38, 171, 64, 14);
        contentPane.add(lblAltezza);

        textField_3 = new JTextField();
        textField_3.setColumns(10);
        textField_3.setBounds(127, 169, 229, 20);
        contentPane.add(textField_3);

        textField_4 = new JTextField();
        textField_4.setColumns(10);
        textField_4.setBounds(127, 200, 229, 20);
        contentPane.add(textField_4);

        JLabel lblPeso = new JLabel("Peso");
        lblPeso.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblPeso.setBounds(38, 203, 46, 14);
        contentPane.add(lblPeso);

        JLabel lblSesso = new JLabel("Sesso");
        lblSesso.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblSesso.setBounds(38, 233, 46, 14);
        contentPane.add(lblSesso);

        textField_5 = new JTextField();
        textField_5.setColumns(10);
        textField_5.setBounds(127, 231, 229, 20);
        contentPane.add(textField_5);

        JLabel lblEmail = new JLabel("email");
        lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblEmail.setBounds(38, 283, 46, 14);
        contentPane.add(lblEmail);

        textField_6 = new JTextField();
        textField_6.setColumns(10);
        textField_6.setBounds(127, 281, 229, 20);
        contentPane.add(textField_6);

        JLabel lblPassword = new JLabel("Password");
        lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblPassword.setBounds(38, 343, 64, 14);
        contentPane.add(lblPassword);

        passwordField = new JPasswordField();
        passwordField.setBounds(127, 341, 229, 20);
        contentPane.add(passwordField);

        textField_7 = new JTextField();
        textField_7.setColumns(10);
        textField_7.setBounds(127, 312, 229, 20);
        contentPane.add(textField_7);

        JLabel lblUsername = new JLabel("Username");
        lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblUsername.setBounds(38, 314, 64, 14);
        contentPane.add(lblUsername);

        JButton btnRegistrami = new JButton("Registrami");
        btnRegistrami.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Atleta atleta= new Atleta();
                try {
                    Writer(atleta);
                } catch (JDOMException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });

        btnRegistrami.setBounds(186, 387, 110, 23);
        contentPane.add(btnRegistrami);
    }

    public static void Writer(Atleta atleta) throws JDOMException, IOException {
         Document document = null;
            Element root = null;

            File xmlFile = new File("db.xml");
            if(xmlFile.exists()) {
                // try to load document from xml file if it exist
                // create a file input stream
                FileInputStream fis = new FileInputStream(xmlFile);
                // create a sax builder to parse the document
                SAXBuilder sb = new SAXBuilder();
                // parse the xml content provided by the file input stream and create a Document object
                document = sb.build(fis);
                // get the root element of the document
                root = document.getRootElement();
                fis.close();
            } else {
                // if it does not exist create a new document and new root
                document = new Document();
                root = new Element("Ercole");
            }

            String nome = textField.getText();
            String cognome = textField_1.getText();
            @SuppressWarnings("deprecation")
            String password = passwordField.getText();
            String username = textField_7.getText();
            String email = textField_6.getText();
            String nascita = textField_2.getText();
            String peso = textField_4.getText();
            String sesso = textField_5.getText();
            String altezza = textField_3.getText();


            Element child = new Element("Atleta");
            child.addContent(new Element("Password").setText(password));
            child.addContent(new Element("Username").setText(username));
            child.addContent(new Element("email").setText(email));
            child.addContent(new Element("Nome").setText(nome));
            child.addContent(new Element("Cognome").setText(cognome));
            child.addContent(new Element("ID").setText("da modificare!"));
            child.addContent(new Element("Nascita").setText(nascita));
            child.addContent(new Element("Peso").setText(peso));
            child.addContent(new Element("Sesso").setText(sesso));
            child.addContent(new Element("Altezza").setText(altezza));
            root.addContent(child);
            document.setContent(root);
            try {
                FileWriter writer = new FileWriter("c:\\db.xml");
                XMLOutputter outputter = new XMLOutputter();
                //outputter.setFormat(Format.getPrettyFormat());
                outputter.output(document, writer);
                outputter.output(document, System.out);
                writer.close(); // close writer
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

我认为您正在阅读的文件不是您正在撰写的文件

您阅读“db.xml”

你写“c:\\ db.xml”

第一个可能永远不存在,因此它没有内容可以附加您的新注册,因此您最终将新注册写入c:\ db.xml文件