I am using
below jaxb library.
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
public static String marshal(Class<?> clazz, Object obj) throws JAXBException { JAXBContext contextObj = JAXBContext.newInstance(clazz); Marshaller marshallerObj = contextObj.createMarshaller(); marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); ByteArrayOutputStream bos = new ByteArrayOutputStream(); marshallerObj.marshal(obj, bos); return new String(bos.toByteArray()); }
Below
snippet converts xml string to java object.
@SuppressWarnings("unchecked") public static <T> T unmarshal(Class<T> clazz, String data) throws JAXBException { JAXBContext contextObj = JAXBContext.newInstance(clazz); InputStream in = new ByteArrayInputStream(data.getBytes()); Unmarshaller unmarshaller = contextObj.createUnmarshaller(); return (T) unmarshaller.unmarshal(in); }
Find the
utiltity class.
package com.sample.app.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; public class JaxbUtil { public static String marshal(Class<?> clazz, Object obj) throws JAXBException { JAXBContext contextObj = JAXBContext.newInstance(clazz); Marshaller marshallerObj = contextObj.createMarshaller(); marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); ByteArrayOutputStream bos = new ByteArrayOutputStream(); marshallerObj.marshal(obj, bos); return new String(bos.toByteArray()); } @SuppressWarnings("unchecked") public static <T> T unmarshal(Class<T> clazz, String data) throws JAXBException { JAXBContext contextObj = JAXBContext.newInstance(clazz); InputStream in = new ByteArrayInputStream(data.getBytes()); Unmarshaller unmarshaller = contextObj.createUnmarshaller(); return (T) unmarshaller.unmarshal(in); } }
Employee.java
package com.sample.app.model; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "employee") public class Employee { private int id; private String firstName; private String lastName; private Address address; public int getId() { return id; } public void setId(int 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 Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Employee [id=").append(id).append(", firstName=").append(firstName).append(", lastName=") .append(lastName).append(", address=").append(address).append("]"); return builder.toString(); } }
App.java
package com.sample.app; import javax.xml.bind.JAXBException; import com.sample.app.model.Address; import com.sample.app.model.Employee; import com.sample.app.util.JaxbUtil; public class App { public static void main(String args[]) throws JAXBException { Employee emp = new Employee(); emp.setId(1); emp.setFirstName("krishna"); emp.setLastName("Majety"); Address addr = new Address(); addr.setCity("Chirala"); addr.setCountry("India"); addr.setState("Andhra Pradesh"); emp.setAddress(addr); String serialized = JaxbUtil.marshal(Employee.class, emp); Employee emp1 = JaxbUtil.unmarshal(Employee.class, serialized); System.out.println(serialized); System.out.println(emp1); } }
Run
App.java, you can see below messages in console.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <employee> <address> <city>Chirala</city> <country>India</country> <state>Andhra Pradesh</state> </address> <firstName>krishna</firstName> <id>1</id> <lastName>Majety</lastName> </employee> Employee [id=1, firstName=krishna, lastName=Majety, address=Address [city=Chirala, state=Andhra Pradesh, country=India]]
No comments:
Post a Comment