@XmlRootElement
public class Book {
private String title;
private String author;
public String getTitle() {
return title;
}
@XmlElement
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
@XmlElement
public void setAuthor(String author) {
this.author = author;
}
}
public class JAXBExample {
public static void main(String[] args) {
try {
JAXBContext context = JAXBContext.newInstance(Book.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Book book = new Book();
book.setTitle("Java Programming");
book.setAuthor("John Doe");
marshaller.marshal(book, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
public class JAXBExample {
public static void main(String[] args) {
try {
JAXBContext context = JAXBContext.newInstance(Book.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
File xmlFile = new File("book.xml");
Book book = (Book) unmarshaller.unmarshal(xmlFile);
System.out.println(book.getTitle());
System.out.println(book.getAuthor());
} catch (JAXBException e) {
e.printStackTrace();
}
}
}