Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Convert Object to XML using JAXB

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 478
    Comment on it

    Hello Programmers,

    A number of times, we need to cast our Java object into an XML or vice-versa. The turms are technically know as marshalling and un-marshalling. There are many different Java libraries to do the same in which Castor and JAXB are commonly used.

    I am going to use JAXB as this comes with our JDK 1.6+ package.

    Let understand with an example. Suppose we have a company with a number of its employees. So, we all might have similar class hierarchy in our mind to represent it. 

    package com.evontech.blog;
    
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class Company {
    	
    	private EmployeeList employeeList;
    
    	public EmployeeList getEmployeeList() {
    		return employeeList;
    	}
    
    	@XmlElement
    	public void setEmployeeList(EmployeeList employeeList) {
    		this.employeeList = employeeList;
    	}
    	
    }
    

    In the code snippet above, Annotation @XmlRootElement describe as this class will be the root of the XML. @XmlElement is an element for that root.

    That's it. Let's test it.

    package com.evontech.blog;
    
    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;
    
    import org.junit.Test;
    
    public class BlogTest {
    
    	@Test
    	public void testJaxb() {
    
    		List<Employee> empoyees = new ArrayList<Employee>();
    
    		Employee emp1 = new Employee();
    		emp1.setId(1);
    		emp1.setName("Dinesh");
    		empoyees.add(emp1);
    
    		Employee em2 = new Employee();
    		em2.setId(2);
    		em2.setName("Babita");
    		empoyees.add(em2);
    		
    		EmployeeList empList = new EmployeeList();
    		empList.setEmployees(empoyees);
    
    		Company company = new Company();
    		company.setEmployeeList(empList);
    
    		try {
    
    			File file = new File("D:\\file.xml");
    			if(!file.exists()) {
    				file.createNewFile();
    			}
    			JAXBContext jaxbContext = JAXBContext.newInstance(Company.class);
    			Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    
    			// output pretty printed
    			jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    
    			jaxbMarshaller.marshal(company,file);
    		} catch(Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    }
    

    Just run this test case. If you will check the file in your D drive.

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <company>
        <employeeList>
            <employees>
                <id>1</id>
                <name>Dinesh</name>
            </employees>
            <employees>
                <id>2</id>
                <name>Babita</name>
            </employees>
        </employeeList>
    </company>

    Thanks :)

    Happy programming.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: