What is the role of the @XmlElement annotation in JAXB?

What is the role of the @XmlElement annotation in JAXB?

A) To map a Java field to an XML element
B) To specify the root element of the XML document
C) To define the structure of the XML document
D) To define the type of XML elements

Answer:

A) To map a Java field to an XML element

Explanation:

The @XmlElement annotation in JAXB is used to map a Java field or property to an XML element. It is commonly used to specify how individual fields of a Java object should be represented in the corresponding XML document.

For example:


@XmlRootElement
public class User {
    @XmlElement
    private String name;

    @XmlElement
    private int age;

    // Getters and setters
}

In this example, the name and age fields of the User class are mapped to XML elements.

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