ciao!
mi stavo studiando un pò jaxb, e volevo esportare una jtable in xml.
questo il model:
codice:
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "book")
public class BookModel {
private int id;
private String title;
private String author;
private String editor;
private String isbn;
private double price;
private String note;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getEditor() {
return editor;
}
public void setEditor(String editor) {
this.editor = editor;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
}
poi ho creato una classe per settare la lista dei libri:
codice:
@XmlRootElement(name = "books")
public class ListBooks {
@XmlElementWrapper(name = "bookList")
ArrayList<BookModel> bookList;
public void setBookList(ArrayList<BookModel> bookList) {
this.bookList = bookList;
}
public ArrayList<BookModel> getBookList() {
return bookList;
}
}
infine lancio tutto così:
codice:
public class ExportXml {
public void createXml(JTable table, File file) throws JAXBException {
ArrayList<BookModel> bookList = new ArrayList<>();
for (int row = 0; row < table.getRowCount(); row++) {
BookModel bookModel = new BookModel();
for (int column = 0; column < table.getColumnCount(); column++) {
bookModel.setId(Integer.parseInt(table.getValueAt(row, 0).toString()));
bookModel.setTitle(table.getValueAt(row, 1).toString());
bookModel.setAuthor(table.getValueAt(row, 2).toString());
bookModel.setEditor(table.getValueAt(row, 3).toString());
bookModel.setPrice(Double.parseDouble(table.getValueAt(row, 4).toString()));
bookModel.setIsbn(table.getValueAt(row, 5).toString());
bookModel.setNote(table.getValueAt(row, 6).toString());
}
bookList.add(bookModel);
}
ListBooks books = new ListBooks();
books.setBookList(bookList);
JAXBContext context = JAXBContext.newInstance(ListBooks.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(books, file);
}
}
il problema è che ottengo questa eccezione:
codice:
1 counts of IllegalAnnotationExceptions
ma non riesco a capire dove sto sbagliando.
qualche idea??