Skip to content
Advertisement

XML to java object using jaxb Unmarshalling namespace

I have below employee.xml which I am trying to convert to Java Object using JAXB. I am getting null value here, please guide me what I am missing here or doing wrong.

I am not familiar with JAXB and namespace <employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schema.cs.csg.com/cs/ib/cpm"> I thing I am missing some annotation or not using it properly.

Output:

Employee [id=null, firstName=null, lastName=null, department=null]

employee.xml

<?xml version="1.0"?>
<employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlnx="http://schema.cs.csg.com/cs/ib/cpm">
    <department>
        <id>101</id>
        <name>IT</name>
    </department>
    <firstName>Rakesh</firstName>
    <id>1</id>
    <lastName>Yadav</lastName>
</employee>

Employee.java

package com.cs.xmltojava

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", namespace="http://schema.cs.csg.com/cs/ib/cpm")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Employee implements Serializable {
 
    private static final long serialVersionUID = 1L;
     
    private Integer id;
    private String firstName;
    private String lastName;
    private Department department;
     
    public Employee() {
        super();
    }
 
    public Employee(int id, String fName, String lName, Department department) {
        super();
        this.id = id;
        this.firstName = fName;
        this.lastName = lName;
        this.department = department;
    }
 
    //Setters and Getters
    public Integer getId() {
        return id;
    }
 
    public void setId(String id) {
        this.id = id;
    }
    
    public String getFirstName() {
        return firstName;
    }
 
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    
    public String getLastName() {
        return lastName;
    }
 
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Department getDepartment() {
        return department;
    }
 
    public void setDepartment(Department department) {
        this.department = department;
    }
    
    @Override
    public String toString() {
        return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", department="+ department + "]";
    }
}

Department.java

package com.cs.xmltojava

import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "department", namespace="http://schema.cs.csg.com/cs/ib/cpm")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Department implements Serializable {
     
    private static final long serialVersionUID = 1L;
     
    Integer id;
    String name;
     
    public Department() {
        super();
    }
 
    public Department(Integer id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
     
    //Setters and Getters
    public Integer getId() {
        return id;
    }
 
    public void setId(String id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
    
    @Override
    public String toString() {
        return "Department [id=" + id + ", name=" + name + "]";
    }
} 

EmployeeMain.java

package com.cs.xmltojava

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class EmployeeMain {

    public static void main(String[] args) throws Exception {

        File xmlFile = new File("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();
        }
 
    }
 
}

Advertisement

Answer

create package-info.java in your package and remove

namespace="http://schema.cs.csg.com/cs/ib/cpm" from @XmlRootElement.

package-info.java

@XmlSchema(
    namespace="http://schema.cs.csg.com/cs/ib/cpm",
    elementFormDefault=XmlNsForm.QUALIFIED
)
package com.cs.xmlparser;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement