Convert JavaObject In To XML using Java

package com.java.basicprograms;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class ConvertJavaObjectInToXML {

public static void main(String[] args) {

Item item = new Item();
item.setItemId(101);
item.setItemName("Laptop");
item.setItemDesc("Lenovo Laptop");
item.setItemPrice(25000.0);

try
{
JAXBContext context = JAXBContext.newInstance(Item.class);
    Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(item,new File("E:/item.xml"));
marshaller.marshal(item, System.out);
}
catch(JAXBException  e)
{
e.printStackTrace();
}
//Convert XML into Java Object
try
{
JAXBContext context = JAXBContext.newInstance(Item.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
File file = new File("E:/item.xml");
Item item1 = (Item) unmarshaller.unmarshal(file);
System.out.println(item1);

}
catch(JAXBException e)
{
e.printStackTrace();
}

}

}

Comments

Popular posts from this blog

WebPage Design Using HTML and CSS

How to design webpage using HTML And CSS

Convert XML In To Java Using Java