I think for such complex xml structure, the best way to parse is to write a xsd schema and then transform your xml into java object.
The Below example will not give you exact solution of your problem but just an idea for parsing xml into Java Object using JAXB.
Definetly this example is also not for such complex xml, but might helps you once you write a xsd schema for the same:
Here is employee.xml file:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
<fName>John</fName>
<id>1</id>
<lName>Paul</lName>
<domain>
<id>999</id>
<name>CS</name>
</domain>
</employee>
Employee.java and Domain.java :
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "employee")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String fName;
private String lName;
private Domain domain;
public Employee() {
super();
}
public Employee(int id, String fName, String lName, Domain domain) {
super();
this.id = id;
this.fName = fName;
this.lName = lName;
this.domain = domain;
}
//Setters and Getters
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public Domain getDomain() {
return domain;
}
public void setDomain(Domain domain) {
this.domain = domain;
}
@Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + fName + ",lastName=" + lName +
", department="+ domain + "]";
}
}
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
@XmlRootElement(name = "domain")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Domain implements Serializable {
private static final long serialVersionUID = 1L;
Integer id;
String name;
public Domain() {
super();
}
public Domain(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
//Setters and Getters
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Domain [id=" + id + ", name=" + name + "]";
}
}
JAXB to Parse XML File to Java:
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;
public class ParseXMLFile {
public static void main(String[] args) {
File xmlFile = new File("src/employee.xml");
JAXBContext jaxbContext;
try
{
jaxbContext = JAXBContext.newInstance(Employee.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Employee employee = (Employee) jaxbUnmarshaller.unmarshal(xmlFile);
System.out.println(employee);
}
catch (JAXBException e)
{
e.printStackTrace();
}
}
}
OUTPUT:
Employee [id=1, firstName=John,lastName=Paul, department=Domain [id=999, name=CS]]
Hope you get an idea around the things.