Adding a new Object to the list of Objects based on the attribute of an Object inside the list in JAVA

221 Views Asked by At

I have a list of objects as displayed

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)])

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

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

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.

    List<Registry> entries = new ArrayList<Registry>();
        try {


            File xmlFile = new File("MultipleNS.xml");
            JAXBContext jaxbContext;
            jaxbContext = JAXBContext.newInstance(Registry.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

            Registry xmlentries = (Registry) jaxbUnmarshaller.unmarshal(xmlFile);
            entries.add(xmlentries);
    
    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) 
            }                
         }           
       }
      }catch (JAXBException e) {
            e.printStackTrace();
        } catch (FactoryConfigurationError e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        System.out.println("---------------------------------");
        
        ListIterator<Registry> litr = entries.listIterator();
        while (litr.hasNext()) {
            System.out.println(litr.next());
        }
    }

(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

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)])

Any suggestions or help would be appreciated? PS: Beginner

1

There are 1 best solutions below

0
BATMAN_2008 On

You can look into afterUnmarshal method. This will be triggered after unmarshalling within this you can write the logic what you want to do:

References and examples: https://docs.oracle.com/javase/7/docs/api/javax/xml/bind/Unmarshaller.Listener.html

https://www.tutorialspoint.com/java/xml/javax_xml_bind_unmarshaller.listener_afterunmarshal.htm

https://howtodoinjava.com/jaxb/jaxb-unmarshaller-example/