Sunday 24 February 2019

Marshalling And UnMarshalling in java



Explanation:Marshalling
As I told u earlier, to convert Java Object into an XML Document we are going to use JAXB API. Let’s see the steps to convert Java Object to XML (Marshalling using JAXB API).
Annotate the Java class with
@XmlRootElement annotation
Create a JAXBContext instance
JAXBContext context = JAXBContext.newInstance(Product.class);
Create a Marshaller reference with the help of JAXBContext instance by calling createMarshaller() method.
Marshaller marshaller = context.createMarshaller();
Creating XML object with the help of marshaller reference by calling marshal() method.
marshaller.marshal(Product, System.out);
Explanation:-UnMarshalling
Let’s see the steps to convert XML Object to Java (Unmarshalling using JAXB API)
Create a JAXBContext instance
JAXBContext context = JAXBContext.newInstance(Product.class);
Create a Unmarshaller reference with the help of JAXBContext instance by calling createUnmarshaller() method.
Unmarshaller unmarshaller = context.createUnmarshaller();
Creating Java Object with the help of unmarshaller reference by calling unmarshal() method.
Employee unmarshalledEmployee =(Product)unmarshaller.unmarshal(new File(“Product.xml”));


package govind; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; Product.java ProductObjectToXml.java ProductXmlToObject.java MarshallingUnmarshallingMain.java ================================================================================================================================ @XmlRootElement public class Product { private int productId; private String ProductName; private float price; public Product() { } @XmlAttribute public int getProductId() { return productId; } public void setProductId(int productId) { this.productId = productId; } @XmlElement public String getProductName() { return ProductName; } public void setProductName(String productName) { ProductName = productName; } @XmlElement public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } public Product(int productId, String productName, float price) { super(); this.productId = productId; ProductName = productName; this.price = price; } } =============================================================================================================================== package govind; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class ProductObjectToXml { public void marshallProductData() { try { JAXBContext jaxbcontext = JAXBContext.newInstance(Product.class); Marshaller marshaller = jaxbcontext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true); Product p1=new Product(1001, "Laptop", 45000.0f); marshaller.marshal(p1,new FileOutputStream("product.xml")); marshaller.marshal(p1, System.out); System.out.println("Marshalling SuccessFully"); } catch (JAXBException | FileNotFoundException e) { e.printStackTrace(); } } } ============================================================================================================================ package govind; import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; public class ProductXmlToObject { public void unmarshallProductData() { try { File f=new File("product.xml"); JAXBContext jaxbcontext = JAXBContext.newInstance(Product.class); Unmarshaller unmarshaller = jaxbcontext.createUnmarshaller(); Product p1 = (Product)unmarshaller.unmarshal(f); System.out.println(p1.getProductId()+" "+p1.getProductName()+" "+p1.getPrice()); System.out.println("Unmarshalling Successfully"); } catch (JAXBException e) { e.printStackTrace(); } } } =============================================================================================================================== package govind; public class MarshallingUnmarshallingMain { public static void main(String[] args) { ProductObjectToXml p1=new ProductObjectToXml(); p1.marshallProductData(); ProductXmlToObject p2=new ProductXmlToObject(); p2.unmarshallProductData(); } } o/p:------------------------ <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <product productId="1001"> <price>45000.0</price> <productName>Laptop</productName> </product> Marshalling SuccessFully 1001 Laptop 45000.0 Unmarshalling Successfully

No comments:

Post a Comment