Monday, 24 March 2014

Learn Java: Marshaling java code to xml using JaxBContext

Learn Java: Marshaling java code to xml using JaxBContext: try {   File file = new File ( "C: \\ file.xml" ) ;  JAXBContext jaxbContext = JAXBContext. newInstance ( Customer. cl...

Unmarshaling Using JaxBContext


try  
{
 File file = new File("C:\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
 Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
 Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
 System.out.println(customer);    
}
catch (JAXBException e)
 {
 e.printStackTrace();
  }

Marshaling java code to xml using JaxBContext

try {

 File file = new File("C:\\file.xml");
 JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
  Marshaller jaxbMarshaller = jaxbContext.createMarshaller();   // output pretty printed jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);   jaxbMarshaller.marshal(customer, file);
 jaxbMarshaller.marshal(customer, System.out);

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