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.