如何将 ArrayList 写入 XML 文件? [英] How to write an ArrayList to an XML file?
本文介绍了如何将 ArrayList 写入 XML 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将 ArrayList
存储到 XML 文件中,以便稍后检索信息,然后将其显示回控制台.
I'm trying to store an ArrayList
into an XML file so that I can retrieve the information later on and then display it back into the console.
有人能告诉我最有效的方法吗?
Can someone show me the most effective way to do this?
这是我试图写入外部文件的内容
Heres what I am trying to write into an external file
// 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
,然后我想存储他们的信息,以便我稍后回来重新显示.
This all creates a Bank user, which gets thrown into an ArrayList
, then I want to store their information so that I can come back later and redisplay it.
推荐答案
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);
}
//根据您的需要修改下面的类
// Modify this below class as per your need
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");
}
}
这篇关于如何将 ArrayList 写入 XML 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文