java - Can I use a loop in my XML output method? -
the method below supposed take stored students , generate xml file data. xml structured correctly both entries same. instead of getting data student1 , student2 student2 twice in row. 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); (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){ } }
this code works fine, problem in code didn't show us: class student
- declared fields there static
means there's 1 copy , when create "multiple students" each new student overrides values of previous one.
remove static
keyword before members in class student (name, address , etc) , code work. here's working example few minor modifications of code:
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); (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 content 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 = ""; }
output file:
<?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>
Comments
Post a Comment