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

5
  • 1
    stackoverflow.com/questions/11624220/… I think above post should be able to help you. Commented Aug 24, 2021 at 7:21
  • Did the answer work for you? Commented Aug 24, 2021 at 12:42
  • i had a workaround done with the for loops itself, I read through the links though Commented Aug 24, 2021 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. Commented Aug 24, 2021 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 --stackoverflow.com/questions/68920060/… Commented Aug 25, 2021 at 9:16

1 Answer 1

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/

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.