public class Person {
private String name;
private int age;
@XmlElement
public String getName() {
return name;
}
@XmlElement
public int getAge() {
return age;
}
}
<root>
<Person>
<name>John</name>
<age>30</age>
</Person>
</root>
public class Main {
public static void main(String[] args) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Person.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
File xmlFile = new File("data.xml");
Person person = (Person) unmarshaller.unmarshal(xmlFile);
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
} catch (JAXBException e) {
e.printStackTrace();
}
}
}