gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:47:33 29 4
gpt4 key购买 nike

我在 xml 中创建了一个数据库,我想在 Jframe 中创建一个注册表单以将对象添加到数据库中。因此,我构建了代码,但每次单击 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();
}
}
}

最佳答案

我认为您正在读取的文件不是您正在写入的文件

您阅读了“db.xml”

你写“c:\\db.xml”

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

关于java - JDOM 将内容写入现有文件而不删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20249072/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com