1

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

BATMAN_2008
  • 2,788
  • 3
  • 31
  • 98
  • 1
    https://stackoverflow.com/questions/11624220/java-adding-elements-to-list-while-iterating-over-it/11624295 I think above post should be able to help you. – aeby Aug 24 '21 at 07:21
  • Did the answer work for you? – BATMAN_2008 Aug 24 '21 at 12:42
  • i had a workaround done with the for loops itself, I read through the links though – Rakshan Premsagar Kapikad Aug 24 '21 at 13:23
  • I believe to do it in the main method after completing the whole unmarshalling process is not a wise choice I believe. Better to use `aftermarshal` so you can do the process as and when you obtain it. – BATMAN_2008 Aug 24 '21 at 17:17
  • I could really not follow certain things there actually, but if you have a sample example, could you provide it in this link here? I guess that would help for me --https://stackoverflow.com/questions/68920060/umarshalling-the-xml-and-transferring-some-new-data-to-a-new-xml-in-java – Rakshan Premsagar Kapikad Aug 25 '21 at 09:16

1 Answers1

2

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/

BATMAN_2008
  • 2,788
  • 3
  • 31
  • 98