Which method is used to marshal a Java object to XML using JAXB?

Which method is used to marshal a Java object to XML using JAXB?

A) marshal()
B) serialize()
C) convertToXML()
D) writeToXML()

Answer:

A) marshal()

Explanation:

The marshal() method in JAXB is used to convert a Java object to an XML document. This process is known as marshalling. The marshal() method writes the Java object to an XML file, a string, or another output format.

For example:


JAXBContext context = JAXBContext.newInstance(User.class);
Marshaller marshaller = context.createMarshaller();
User user = new User("John", 30);
marshaller.marshal(user, new File("user.xml"));

In this example, the User object is marshalled to an XML file named user.xml.

Reference links:

https://www.javaguides.net/p/java-xml-tutorial.html

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top