import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root(name = "book")
public class Book {
@Element
private String title;
@Element
private String author;
@Element
private int year;
public Book(String title, String author, int year) {
this.title = title;
this.author = author;
this.year = year;
}
// Getters and setters
}
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
public class Main {
public static void main(String[] args) {
Book book = new Book("Java Programming", "John Doe", 2022);
Serializer serializer = new Persister();
try {
File file = new File("book.xml");
serializer.write(book, file);
System.out.println("XML document generated successfully!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
<book>
<title>Java Programming</title>
<author>John Doe</author>
<year>2022</year>
</book>