What is the purpose of the @XmlAttribute
annotation in JAXB?
A) To map a Java field to an XML attribute
B) To map a Java field to an XML element
C) To define the root element of the XML document
D) To define the namespace of an XML element
Answer:
A) To map a Java field to an XML attribute
Explanation:
The @XmlAttribute
annotation in JAXB is used to map a Java field or property to an XML attribute. This annotation specifies that the annotated field should be represented as an attribute in the resulting XML document.
For example:
@XmlRootElement
public class User {
@XmlAttribute
private int id;
@XmlElement
private String name;
// Getters and setters
}
In this example, the id
field is mapped to an XML attribute, while the name
field is mapped to an XML element.