Which method is used to unmarshal an XML document to a Java object using JAXB?
A)
unmarshal()
B)
deserialize()
C)
convertToObject()
D)
readFromXML()
Answer:
A)
unmarshal()
Explanation:
The unmarshal()
method in JAXB is used to convert an XML document to a Java object. This process is known as unmarshalling. The unmarshal()
method reads the XML content and populates the fields of the corresponding Java object.
For example:
JAXBContext context = JAXBContext.newInstance(User.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
User user = (User) unmarshaller.unmarshal(new File("user.xml"));
In this example, the XML file user.xml
is unmarshalled into a User
object.