gpt4 book ai didi

java - 我可以在 XML 输出方法中使用循环吗?

转载 作者:行者123 更新时间:2023-11-30 03:19:57 24 4
gpt4 key购买 nike

下面的方法应该获取所有存储的学生并生成包含数据的 XML 文件。 XML 的结构正确,但两个条目是相同的。我没有获取 Student1 和 Student2 的数据,而是连续两次获取 Student2 。我在这里想念什么?

public void exportStudentXML(ArrayList <Student> studentListIn ){
ArrayList <Student> studentList = studentListIn;

DocumentBuilderFactory myDocBuilderFactory = DocumentBuilderFactory.newInstance();

try{
DocumentBuilder myDocBuilder = myDocBuilderFactory.newDocumentBuilder();
Document documentModel = myDocBuilder.newDocument();

Element root = documentModel.createElement("studentList");
documentModel.appendChild(root);

for (Student thisStudent : studentList){
Element listElement = documentModel.createElement("student");
root.appendChild(listElement);

Element nameElement = documentModel.createElement("name");
Text nameText = documentModel.createTextNode(thisStudent.name);
nameElement.appendChild(nameText);
listElement.appendChild(nameElement);

Element addressElement = documentModel.createElement("address");
Text addressText = documentModel.createTextNode(thisStudent.address);
addressElement.appendChild(addressText);
listElement.appendChild(addressElement);

Element ssnElement = documentModel.createElement("ssn");
Text ssnText = documentModel.createTextNode(thisStudent.socialSecurityNumber);
ssnElement.appendChild(ssnText);
listElement.appendChild(ssnElement);

Element dobElement = documentModel.createElement("dob");
Text dobText = documentModel.createTextNode(thisStudent.toStringDOB());
dobElement.appendChild(dobText);
listElement.appendChild(dobElement);

Element dogElement = documentModel.createElement("dog");
Text dogText = documentModel.createTextNode(thisStudent.toStringDOG());
dogElement.appendChild(dogText);
listElement.appendChild(dogElement);

Element gpaElement = documentModel.createElement("gpa");
Text gpaText = documentModel.createTextNode(thisStudent.toStringGPA());
gpaElement.appendChild(gpaText);
listElement.appendChild(gpaElement);

}
OutputFormat formatToOutput = new OutputFormat(documentModel);

formatToOutput.setIndenting(true);
XMLSerializer serializer = new XMLSerializer( new FileOutputStream( new File("studentlist.xml")), formatToOutput );
serializer.serialize(documentModel);

}catch(Exception e){



}

}

最佳答案

这段代码工作正常,问题出在您没有向我们展示的代码中:类Student - 您将其中的所有字段声明为static,这意味着只有一份副本,当您创建“多个学生”时,每个新学生都会覆盖前一个学生的值。

删除 Student 类中所有成员(姓名、地址等)之前的 static 关键字,您的代码将正常工作。这是一个工作示例,对代码进行了一些细微的修改:

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
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;

class StudentsToXML {

public static void main(String[] args) {
ArrayList <Student> studentList = new ArrayList<>();
studentList.add(new Student("John", "CA", "123"));
studentList.add(new Student("Mark", "AZ", "456"));
exportStudentXML(studentList);
}

public static void exportStudentXML(ArrayList <Student> studentList ){
DocumentBuilderFactory myDocBuilderFactory = DocumentBuilderFactory.newInstance();

try{
DocumentBuilder myDocBuilder = myDocBuilderFactory.newDocumentBuilder();
Document documentModel = myDocBuilder.newDocument();

Element root = documentModel.createElement("studentList");
documentModel.appendChild(root);

for (Student thisStudent : studentList){
Element listElement = documentModel.createElement("student");
root.appendChild(listElement);

Element nameElement = documentModel.createElement("name");
nameElement.appendChild(documentModel.createTextNode(thisStudent.name));
listElement.appendChild(nameElement);

Element addressElement = documentModel.createElement("address");
addressElement.appendChild(documentModel.createTextNode(thisStudent.address));
listElement.appendChild(addressElement);

Element ssnElement = documentModel.createElement("ssn");
ssnElement.appendChild(documentModel.createTextNode(thisStudent.socialSecurityNumber));
listElement.appendChild(ssnElement);

}
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(documentModel);
StreamResult result = new StreamResult(new File("studentlist.xml"));

transformer.transform(source, result);
System.out.println("File saved!");

}catch(Exception e){
e.printStackTrace()
}
}
}


class Student {

Student (String name, String addr, String soc) {
this.name = name;
address = addr;
socialSecurityNumber = soc;
}
static String name = "";
static String address = "";
static String socialSecurityNumber = "";

}

输出文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<studentList>
<student><name>John</name><address>CA</address><ssn>123</ssn></student>
<student><name>Mark</name><address>AZ</address><ssn>456</ssn></student>
</studentList>

关于java - 我可以在 XML 输出方法中使用循环吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31569026/

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