For testing purposes, I used used JAXB to generate an XML from an Object. This work fine. The code is below.
package com.mns.mnsutilities.jaxb.model;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement(name="Emp_MNS")
@XmlType(propOrder= {"name", "age", "role", "gender", "addressesList"})
public class Employee {
private int id;
private String gender;
private int age;
private String name;
private String role;
private String password;
private List<Address> addressesList;
public Employee() {}
public Employee(int id, String gender, int age, String name, String role,
String password) {
super();
this.id = id;
this.gender = gender;
this.age = age;
this.name = name;
this.role = role;
this.password = password;
}
public Employee(int id, String gender, int age, String name, String role,
String password, List<Address> addressesList) {
super();
this.id = id;
this.gender = gender;
this.age = age;
this.name = name;
this.role = role;
this.password = password;
this.addressesList = addressesList;
}
@XmlAttribute
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement(name = "gen")
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// @XmlNullPolicy(emptyNodeRepresentsNull = true, nullRepresentationForXml = XmlMarshalNullRepresentation.EMPTY_NODE)
@XmlElement(nillable=true)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
@XmlTransient
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@XmlElement(name = "addresses")
public List<Address> getAddressesList() {
return addressesList;
}
public void setAddressesList(List<Address> addressesList) {
this.addressesList = addressesList;
}
@Override
public String toString() {
return "Employee [id=" + id + ", gender=" + gender + ", age=" + age
+ ", name=" + name + ", role=" + role + ", password="
+ password + ", addressesList=" + addressesList + "]";
}
}
And:
package com.mns.mnsutilities.jaxb.model;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(namespace="")
public class Address {
private String street;
private String city;
private String zipCode;
private String country;
public Address() {}
public Address(String street, String city, String zipCode, String country) {
super();
this.street = street;
this.city = city;
this.zipCode = zipCode;
this.country = country;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return "Address [street=" + street + ", city=" + city + ", zipCode="
+ zipCode + ", country=" + country + "]";
}
}
My main Class is :
package com.mns.mnsutilities.jaxb.batch;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import com.mns.mnsutilities.jaxb.model.Address;
import com.mns.mnsutilities.jaxb.model.Employee;
public class LaunchAction {
private static final String FILE_NAME = "output/CT3D_XML_SAMPLE_FINAL.xml";
public static void main(String[] args) {
Employee emp = new Employee();
emp.setId(1);
emp.setAge(25);
emp.setName("Yovan");
emp.setGender("Male");
emp.setRole("Developer");
emp.setPassword("sensitive");
List<Address> addressesList = new ArrayList<>();
Address address1 = new Address("Main Road", "Ebene", "11111", "Mauritius");
Address address2 = new Address("Royal Road", "Rose-Hill", "2222", "Mauritius");
addressesList.add(address1);
addressesList.add(address2);
emp.setAddressesList(addressesList);
jaxbObjectToXML(emp);
Employee empFromFile = jaxbXMLToObject();
System.out.println(empFromFile.toString());
}
private static Employee jaxbXMLToObject() {
try {
JAXBContext context = JAXBContext.newInstance(Employee.class);
Unmarshaller un = context.createUnmarshaller();
Employee emp = (Employee) un.unmarshal(new File(FILE_NAME));
return emp;
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}
private static void jaxbObjectToXML(Employee emp) {
try {
JAXBContext context = JAXBContext.newInstance(Employee.class);
Marshaller m = context.createMarshaller();
//for pretty-print XML in JAXB
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
// Write to System.out for debugging
m.marshal(emp, System.out);
// Write to File
m.marshal(emp, new File(FILE_NAME));
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
The XML output is :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Emp_MNS id="1">
<name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true">
</name>
<age>25</age>
<role>Developer</role>
<gen>Juggoo</gen>
<addresses>
<city>Ebene</city>
<country>Mauritius</country>
<street>Main Road</street>
<zipCode>11111</zipCode>
</addresses>
<addresses>
<city>Rose-Hill</city>
<country>Mauritius</country>
<street>Royal Road</street>
<zipCode>2222</zipCode>
</addresses>
</Emp_MNS>
What I really would like to have is :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Emp_MNS id="1">
<name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true">
</name>
<age>25</age>
<role>Developer</role>
<gen>Juggoo</gen>
<addresses>
**<address>**
<city>Ebene</city>
<country>Mauritius</country>
<street>Main Road</street>
<zipCode>11111</zipCode>
**</address>**
**<address>**
<city>Rose-Hill</city>
<country>Mauritius</country>
<street>Royal Road</street>
<zipCode>2222</zipCode>
**</address>**
</addresses>
</Emp_MNS>
Could you please guide me on how to proceed?