Ofcourse, I would gladly take your advice. Infact, I would really appreciate because I am just starting and would be guided in the right direction. I will start naming them currently in a manner that is understandable.
First and foremost, yes so there is an input xml file that has been parsed using JAXB standard.
Registry.java
[CODE=java]import lombok.Data;
@XmlRootElement(name="Registry")//,namespace = "http://www.registar.com")
@XmlAccessorType(XmlAccessType.FIELD)
@Data
public class Registry {
@XmlElement(name="Student")//,namespace = "http://www.registar.com")
private List<Student> students;
}[/CODE]
Student.java
[CODE=java]@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class Student {
@XmlAttribute(name = "Gender")
private String Gender;
@XmlAttribute(name = "School")
private String School;
@XmlElement(name = "FirstName")//namespace = "http://www.registar.com")
private String FirstName;
@XmlElement(name = "Value")
private Value Value;
}
[/CODE]
So in my main parser now, I search for the student having school at Berlin, and I add one student there( practically it doesnt make sense, but just for me to understand and get familiar)
[CODE=java]public class Mainparser {
public static void main(String[] args) {
try {
File xmlFile = new File("MultipleNS.xml");
JAXBContext jaxbContext;
jaxbContext = JAXBContext.newInstance(Registry.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Registry xmlentries = (Registry) jaxbUnmarshaller.unmarshal(xmlFile);
Student newStudent = null;
List<Student> students = xmlentries.getStudents();
for (Student s : students) {
if (s.getSchool().equals("Berlin")) {
newStudent=new Student();
newStudent.setFirstName("MP");
newStudent.setGender("F");
newStudent.setSchool("Berlin");
break;
}
}
if(newStudent!=null) {
students.add(newStudent);
}
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(xmlentries, System.out);
}catch (JAXBException e) {
e.printStackTrace();
} catch (FactoryConfigurationError e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}[/CODE]
I could achieve the result as below
[CODE=xml]<Registry xmlns:ms="http://www.registar.com/ScoreVariant" xmlns="http://www.registar.com">
<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>
<Student Gender="F" School="Berlin">
<FirstName>MP</FirstName>
</Student>
</Registry>[/CODE]