Reputation: 45
I am working in create simple student details task for my practice.
I have the following class as StudentModel
@XmlRootElement(name = "Student")
@XmlType(name = "StudentDetails",propOrder = { "studentName", "fatherName", "rollNumber", "studentAge",
"studentGender", "studentDOB", "addressLine1", "addressLine2", "city",
"state", "country", "zipcode" })
public class StudentModel {
@XmlElement
private String studentName;
@XmlElement
private String fatherName;
@XmlAttribute
private int rollNumber;
@XmlElement
private int studentAge;
@XmlElement
private String studentGender;
@XmlElement
private String studentDOB;
@XmlElement
private String addressLine1;
@XmlElement
private String addressLine2;
@XmlElement
private String city;
@XmlElement
private String state;
@XmlElement
private String country;
@XmlElement
private int zipcode;
}
Following code i used to create xml file from java objects
if( xml.exists() ){
try {
fr4 = new FileReader(file);
br4 = new BufferedReader(fr4);
while( (readFile = br4.readLine()) != null ){
line = new Scanner(readFile).useDelimiter(",");
StudentModel toXml = new StudentModel(line.next(),line.next(), Integer.parseInt(line.next()),
Integer.parseInt(line.next()), line.next(), line.next(), line.next(), line.next(),
line.next(), line.next(), line.next(), Integer.parseInt(line.next()));
JAXBContext context;
context = JAXBContext.newInstance(StudentModel.class);
Marshaller marshal = context.createMarshaller();
marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
Boolean.TRUE);
fw2 = new FileWriter(xml, true);
marshal.marshal(toXml, fw2);
}
} catch (JAXBException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else{
System.out.println("File not created");
}
My problem is i couldn't make multiple objects into one file and the output is look like this.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<StudentModel>
<studentName>peter</studentName>
<fatherName>john</fatherName>
<rollNumber>12</rollNumber>
<studentAge>24</studentAge>
<studentGender>m</studentGender>
<studentDOB>16-06-1991</studentDOB>
<addressLine1>ty</addressLine1>
<addressLine2>yt</addressLine2>
<city>new york</city>
<state>ny</state>
<country>us</country>
<zipcode>456123</zipcode>
</StudentModel>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<StudentModel rollNumber="11">
<studentName>john</studentName>
<fatherName>peter</fatherName>
<studentAge>24</studentAge>
<studentGender>M</studentGender>
<studentDOB>02-10-1991</studentDOB>
<addressLine1>line1</addressLine1>
<addressLine2>line2</addressLine2>
<city>washington</city>
<state>dc</state>
<country>us</country>
<zipcode>123456</zipcode>
</StudentModel>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
Upvotes: 1
Views: 1179
Reputation: 2153
what about creating a class on top and add a list of StudentModels to it. So the new class would be the root
@XmlRootElement(name = "Student")
public class Student{
List<StudentModel> studentModels;
}
And xml would be something like
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Student>
<StudentModel>
<studentName>peter</studentName>
<fatherName>john</fatherName>
<rollNumber>12</rollNumber>
<studentAge>24</studentAge>
<studentGender>m</studentGender>
<studentDOB>16-06-1991</studentDOB>
<addressLine1>ty</addressLine1>
<addressLine2>yt</addressLine2>
<city>new york</city>
<state>ny</state>
<country>us</country>
<zipcode>456123</zipcode>
</StudentModel>
<StudentModel rollNumber="11">
<studentName>john</studentName>
<fatherName>peter</fatherName>
<studentAge>24</studentAge>
<studentGender>M</studentGender>
<studentDOB>02-10-1991</studentDOB>
<addressLine1>line1</addressLine1>
<addressLine2>line2</addressLine2>
<city>washington</city>
<state>dc</state>
<country>us</country>
<zipcode>123456</zipcode>
</StudentModel>
</Student>
Upvotes: 2