我正在为我的练习创建简单的学生详细信息任务。
我有以下课程作为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;
}
以下代码用于从java对象创建xml文件
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");
}
我的问题是我无法将多个对象放入一个文件中,输出看起来像这样。
<?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"?>
答案 0 :(得分:2)
如何在顶部创建一个类并向其添加一个StudentModel列表。所以新类将是根
@XmlRootElement(name = "Student")
public class Student{
List<StudentModel> studentModels;
}
xml就像
<?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>