The Method below is supposed to take all the stored students and generate an XML file with the data. As is the XML is structured correctly but both entries are the same. Instead of getting data for student1 and student2 I just get student2 twice in a row. What am I missin here?
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){
}
}