Auf Thema antworten

So to give a complete overview:


I have a list of objects as displayed



[CODE=java]Registry(Student=[Student(Gender=M, School=Hamburg, FirstName=RP, Value=null),

                  Student(Gender=F, School=Berlin, FirstName=SK, Value=null),

                  Student(Gender=M, School=Frankfurt, FirstName=TK, Value=null)])[/CODE]



This represents the XML structure after unmarshalling. The original structure of the XML is shown below



[CODE=xml]<?xml version="1.0" encoding="UTF-8"?>

<Registry xmlns="http://www.registar.com"

          xmlns:ms ="http://www.registar.com/ScoreVariant">

    <Student Gender = "M" School = "Hamburg">

        <FirstName>RP</FirstName>

    </Student>

    <Student Gender = "F" School = "Berlin">

        <FirstName>SK</FirstName>

    </Student>

    <Student Gender = "M" School = "Frankfurt">

        <FirstName>TK</FirstName>

    </Student>

</Registry>[/CODE]



There are classes written for Registry, Student and Value with getter and setter methods (used lombok package)


Now, I want to scan through the list of objects, Look for school location, and if the location is "Berlin", I want to add another student.



 

[CODE=java]  List<Registry> entries = new ArrayList<Registry>();

   

    for (Registry e: entries) {

        for (Student s : e.getStudent()) {

            if (s.getSchool().equals("Berlin")) {

                 Student obj = new Student();

                 obj.setFirstName("MP");

                obj.setGender("F");

                obj.setSchool("Berlin");    // (1)

            }                

        }            

    }[/CODE]



(1) Here I can create new object, but I am not able to add it to the XML (as the registry class has the ```List<Student> student``` as a property.



Ultimately, I want to have a output like the below



[CODE=java]Registry(Student=[Student(Gender=M, School=Hamburg, FirstName=RP, Value=null),

                  Student(Gender=F, School=Berlin, FirstName=SK, Value=null),

                  Student(Gender=F, School=Berlin, FirstName=MP, Value=null),

                  Student(Gender=M, School=Frankfurt, FirstName=TK, Value=null)])[/CODE]



Oben