Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




Java Interview questions: -Will you explain marshaling & unmarshalling in Java?

This is the most practical oriented Java Interview Questions which may be asked during the Interview by the Interviewer.

Marshalling:

Marshalling creates an XML document from a content tree

To marshal a content tree

  • Create a JAXBContext object
  • Create a Marshaller object (Marshaller marshaller = jaxbContext.createMarshaller();)
  • Set required properties using setProperty method of Marshaller marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
  • Call the marshal method marshaller.marshal(collection, new FileOutputStream("jaxbOutput.xml"));

Unmarshalling:

Unmarshalling an XML document means creating a tree of content objects that represents the content and organization of the document

To unmarshal an XML document,

  • Create a JAXBContext object( JAXBContext jaxbContext = JAXBContext.newInstance("package name ");
  • Create an Unmarshaller object Unmarshaller unmarshaller = jc.createUnmarshaller();
  • Call the unmarshal method unmarshaller.unmarshal(new File( "xml name"));
  • Use the get methods in the schema-derived classes to access the XML data

See the following video on Introduction to Hibernate and it concepts: -

Click for more Java interview questions

Regards

Visit for more author’s blog on Java interview questions

Share this article   |    Print    |    Article read by 3663 times
Author:
Shivprasad Koirala
We conduct training for Java/J2EE technologies like design patterns, J2EE, Struts, Hibernate, Spring, Rich Interface etc. and likes to write articles on the same in free time.
Related Articles:
Related Interview Questions: