2015-12-26 38 views
1

我正在爲我的練習創建簡單的學生細節任務。將多個對象編組爲一個xml文件

我有以下類作爲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; 

    } 

下面的代碼我用來創建XML文件從Java對象

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"?>  

回答

2

怎麼樣在頂部創建一個類,並添加一個StudentModels列表。因此,新的類將是根

@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> 
+0

感謝您的答覆@awsome照你說的做了。但我的xml沒有正確地將對象值添加到xml –

+0

你是什麼意思通過正確添加 – awsome

+0

我糾正它。我的意思是列表中的java對象在瀏覽器中無法正確顯示。 –

相關問題