gpt4 book ai didi

java - 如何将 ArrayList 写入 XML 文件?

转载 作者:行者123 更新时间:2023-11-29 10:14:37 25 4
gpt4 key购买 nike

我正在尝试将一个 ArrayList 存储到一个 XML 文件中,以便我可以稍后检索信息,然后将其显示回控制台。

有人能告诉我最有效的方法吗?

编辑:

这是我要写入外​​部文件的内容

// new user is created
Bank bank = new Bank();

System.out.println("Enter your full name below (e.g. John M. Smith): ");
String name = scanner.nextLine();
System.out.println("Create a username: ");
String userName = scanner.nextLine();
System.out.println("Enter your starting deposit amount: ");
int balance = scanner.nextInt();

System.out.print(dash);
System.out.print("Generating your information...\n");
System.out.print(dash);

int pin = bank.PIN();
String accountNum = bank.accountNum();

User user = new User(name, userName, pin, accountNum, balance);

//new user gets added to the array list
Bank.users.add(user);

System.out.println(user);

这一切都创建了一个 Bank 用户,它被扔进了一个 ArrayList,然后我想存储他们的信息,以便稍后返回并重新显示它。

最佳答案

public static void main(String[] args) {
// TODO Auto-generated method stub

WriteFile ob = new WriteFile();
ArrayList list = new ArrayList();
list.add(new details("A", 20, 1));
list.add(new details("B", 30, 2));

ob.writeXmlFile(list);
}

//根据需要修改下面的类

class details {
String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

int age;
int id;

public details() {
}

public details(String name_, int age_, int id_) {
name = name_;
age = age_;
id = id_;
}

//下面类实际写的

public void writeXmlFile(ArrayList<details> list) {

try {

DocumentBuilderFactory dFact = DocumentBuilderFactory.newInstance();
DocumentBuilder build = dFact.newDocumentBuilder();
Document doc = build.newDocument();

Element root = doc.createElement("Studentinfo");
doc.appendChild(root);

Element Details = doc.createElement("Details");
root.appendChild(Details);


for (details dtl : list) {

Element name = doc.createElement("Name");
name.appendChild(doc.createTextNode(String.valueOf(dtl
.getName())));
Details.appendChild(name);

Element id = doc.createElement("ID");
id.appendChild(doc.createTextNode(String.valueOf(dtl.getId())));
Details.appendChild(id);

Element mmi = doc.createElement("Age");
mmi.appendChild(doc.createTextNode(String.valueOf(dtl.getAge())));
Details.appendChild(mmi);

}

// Save the document to the disk file
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();

// format the XML nicely
aTransformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");

aTransformer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "4");
aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");

DOMSource source = new DOMSource(doc);
try {
// location and name of XML file you can change as per need
FileWriter fos = new FileWriter("./ros.xml");
StreamResult result = new StreamResult(fos);
aTransformer.transform(source, result);

} catch (IOException e) {

e.printStackTrace();
}

} catch (TransformerException ex) {
System.out.println("Error outputting document");

} catch (ParserConfigurationException ex) {
System.out.println("Error building document");
}
}

关于java - 如何将 ArrayList 写入 XML 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21125998/

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