gpt4 book ai didi

Java XML DOM 写入新信息而不覆盖以前的信息

转载 作者:太空宇宙 更新时间:2023-11-04 09:13:18 25 4
gpt4 key购买 nike

我正在尝试构建一个日历,我希望将所有约会存储在 XML 文件中,但是,我似乎找不到如何执行此操作。我一直在阅读有类似问题的人的答案,但似乎没有任何效果,这是我拥有的 DOM 类,“createDocument”方法的所有输入都来自文本框:

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;


public class GeneradorDOM {
private Document doc;


public GeneradorDOM() throws ParserConfigurationException {
DocumentBuilderFactory factoria = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factoria.newDocumentBuilder();
doc = builder.newDocument();
}


public void createDocument(String hour, int day, int month, int year, String name,
String lastName, String title, String description,String row, String column) throws IOException{

Element root = doc.getDocumentElement();
Element appointment = doc.createElement("appointment");

/* Instead of root, I have tried with the following code as well:
Element appointments = doc.createElement("appointments");
doc.appendChild(appointments);

Element appointment = doc.createElement("appointment");
appointments.appendChild(appointment);
*/

Element nameElement = doc.createElement("name");
Text nameText = doc.createTextNode(name);
appointment.appendChild(nameElement);
nameElement.appendChild(nameText);
appointment.appendChild(nameElement);

Element lastNameElement = doc.createElement("lastname");
Text lastNameText = doc.createTextNode(lastName);
appointment.appendChild(lastNameElement);
lastNameElement.appendChild(lastNameText);
appointment.appendChild(lastNameElement);

Element dayElement = doc.createElement("day");
Text dayText = doc.createTextNode(Integer.toString(day));
appointment.appendChild(dayElement);
dayElement.appendChild(dayText);


Element monthElement = doc.createElement("month");
Text monthText = doc.createTextNode(Integer.toString(month));
appointment.appendChild(monthElement);
monthElement.appendChild(monthText);

Element yearElement = doc.createElement("year");
Text yearText = doc.createTextNode(Integer.toString(year));
appointment.appendChild(yearElement);
yearElement.appendChild(yearText);

Element hourElement = doc.createElement("hour");
Text hourText = doc.createTextNode(hour);
appointment.appendChild(hourElement);
hourElement.appendChild(hourText);



Element titleElement = doc.createElement("title");
Text titleText = doc.createTextNode(title);
appointment.appendChild(titleElement);
titleElement.appendChild(titleText);

Element descriptionElement = doc.createElement("description");
Text descriptionText = doc.createTextNode(description);
appointment.appendChild(descriptionElement);
descriptionElement.appendChild(descriptionText);

Element rowElement = doc.createElement("row");
Text rowText = doc.createTextNode(row);
appointment.appendChild(rowElement);
rowElement.appendChild(rowText);

Element columnElement = doc.createElement("column");
Text columnText = doc.createTextNode(column);
appointment.appendChild(columnElement);
columnElement.appendChild(columnText);

root.appendChild(appointment);

}

public void generateXML() throws TransformerConfigurationException, IOException, TransformerException{
TransformerFactory factoria = TransformerFactory.newInstance();
Transformer transformer = factoria.newTransformer();

Source source = new DOMSource(doc);
File file = new File("Appointments.xml");
FileWriter fw = new FileWriter(file);
PrintWriter pw = new PrintWriter(fw);

StreamResult result = new StreamResult(new FileOutputStream("Appointments.xml", true));

transformer.transform(source, result);

}
}

最佳答案

这是一个很好的起点,基本上你不能创建一个空的 xml 并读取并解析为 DOM 对象,因为你会看到一个异常。

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

public class PersisterAppointment {

private static String APPOINTMENT_FILE_NAME = "Appointments.xml";

public static void main(String args[]){
PersisterAppointment demo = new PersisterAppointment();
try {
demo.persistAppointment("Edgard","Rendón",Calendar.getInstance(),"Title field","Description field","row field","column field");
demo.persistAppointment("Brandon","Ramirez",Calendar.getInstance(),"Title field","Description field","row field","column field");
} catch (ParserConfigurationException | TransformerException | SAXException | IOException e) {
e.printStackTrace();
}
}

public boolean isAppointment(){
File file = new File(APPOINTMENT_FILE_NAME);
return file.exists();
}

public void persistAppointment(String iname, String ilastname, Calendar calendar, String ititle, String idescription, String irow, String icolumn) throws ParserConfigurationException, TransformerException, SAXException, IOException{
DocumentBuilderFactory docfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docbuilder = docfactory.newDocumentBuilder();
Document doc;

if(isAppointment()){
InputStream is = new FileInputStream(APPOINTMENT_FILE_NAME);
doc = docbuilder.parse(is);
}else{
doc = docbuilder.newDocument();
}

TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer transformer = tfactory.newTransformer();


Source source = new DOMSource(doc);
File file = new File(APPOINTMENT_FILE_NAME);


Element appointment = doc.createElement("appointment");
doc.appendChild(appointment);


Element ename = doc.createElement("name");
Element elastname = doc.createElement("lastname");
Element eday = doc.createElement("day");
Element emonth = doc.createElement("month");
Element eyear = doc.createElement("year");
Element ehour = doc.createElement("hour");
Element etitle = doc.createElement("title");
Element edescription = doc.createElement("description");
Element erow = doc.createElement("row");
Element ecolumn = doc.createElement("column");

Text tname = doc.createTextNode(iname);
Text tlastname = doc.createTextNode(ilastname);
Text tday = doc.createTextNode(String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
Text tmonth = doc.createTextNode(String.valueOf(calendar.get(Calendar.MONTH)));
Text tyear = doc.createTextNode(String.valueOf(calendar.get(Calendar.YEAR)));
Text thour = doc.createTextNode(String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)));
Text ttitle = doc.createTextNode(ititle);
Text tdescription = doc.createTextNode(idescription);
Text trow = doc.createTextNode(irow);
Text tcolumn = doc.createTextNode(icolumn);


ename.appendChild(tname);
elastname.appendChild(tlastname);
eday.appendChild(tday);
emonth.appendChild(tmonth);
eyear.appendChild(tyear);
ehour.appendChild(thour);
etitle.appendChild(ttitle);
edescription.appendChild(tdescription);
erow.appendChild(trow);
ecolumn.appendChild(tcolumn);

appointment.appendChild(ename);
appointment.appendChild(elastname);
appointment.appendChild(eday);
appointment.appendChild(emonth);
appointment.appendChild(eyear);
appointment.appendChild(ehour);
appointment.appendChild(etitle);
appointment.appendChild(edescription);
appointment.appendChild(erow);
appointment.appendChild(ecolumn);

StreamResult result = new StreamResult(file);

transformer.transform(source, result);
}
}
}

您只需要修改代码即可在现有的xml文件中添加新节点

关于Java XML DOM 写入新信息而不覆盖以前的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59422342/

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