Visualizzazione dei risultati da 1 a 7 su 7
  1. #1

    JAXB: 1 counts of IllegalAnnotationExceptions

    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??

  2. #2
    vi posto qualche errore in più:
    codice:
    com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
    Class has two properties of the same name "bookList"
        this problem is related to the following location:
            at public java.util.ArrayList com_mattepuffo_book.ListBooks.getBookList()
            at com_mattepuffo_book.ListBooks
        this problem is related to the following location:
            at java.util.ArrayList com_mattepuffo_book.ListBooks.bookList
            at com_mattepuffo_book.ListBooks
    però non capisco, perchè io la lista la setto una sola volta!!

  3. #3
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Quote Originariamente inviata da fermat Visualizza il messaggio
    il problema è che ottengo questa eccezione:
    codice:
    1 counts of IllegalAnnotationExceptions
    ma non riesco a capire dove sto sbagliando.
    qualche idea??
    Sì ce l'ho. Allora, innanzitutto una premessa. L'accesso alle informazioni negli oggetti da parte di JAXB può avvenire in diversi modi. Se non si specifica niente, il default è dato dalla enum XmlAccessType.PUBLIC_MEMBER che è descritto con:

    Every public getter/setter pair and every public field will be automatically bound to XML, unless annotated by XmlTransient. Fields or getter/setter pairs that are private, protected, or defaulted to package-only access are bound to XML only when they are explicitly annotated by the appropriate JAXB annotations.
    Il tuo campo bookList ha livello di accesso di default (package level). PUBLIC_MEMBER a livello basilare non lo prenderebbe in considerazione .... ma in realtà lo prende in considerazione perché l'hai annotato esplicitamente.

    Quindi JAXB deduce una proprietà "bookList" dal campo e poi la stessa proprietà "bookList" dai getter/setter.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  4. #4
    ciao andbin!

    allora, modificando così funziona:
    codice:
    @XmlRootElement(name = "books")
    public class ListBooks {
    
        ArrayList<BookModel> bookList;
    
        public void setBookList(ArrayList<BookModel> bookList) {
            this.bookList = bookList;
        }
    
        public ArrayList<BookModel> getBookList() {
            return bookList;
        }
    }
    però il file mi esce così:
    codice:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <books>
        <bookList>
            <author>arthur c. clarke</author>
            <editor>longanesi</editor>
            <id>87</id>
            <isbn></isbn>
            <note></note>
            <price>0.0</price>
            <title>2001 odissea nello spazio</title>
        </bookList>
    .......
    </books>
    vorrei però mettere <book> al posto di <bookList>.
    ho provato rimetterendo XmlElementWrapper con un name diverso, ma ottengo lo stesso errore:
    codice:
    @XmlRootElement(name = "books")
    public class ListBooks {
    
        @XmlElementWrapper(name = "book")
        ArrayList<BookModel> bookList;
    
        public void setBookList(ArrayList<BookModel> bookList) {
            this.bookList = bookList;
        }
    
        public ArrayList<BookModel> getBookList() {
            return bookList;
        }
    }
    come posso fare??

  5. #5
    ok rileggendo meglio ho capito che una soluzione è questa:
    codice:
    @XmlRootElement(name = "books")
    public class ListBooks {
    
        ArrayList<BookModel> book;
    
        public void setBook(ArrayList<BookModel> book) {
            this.book = book;
        }
    
        public ArrayList<BookModel> getBook() {
            return book;
        }
    }
    così mi esce come vorrei.
    non so se ci sono altre soluzioni usando le annotations.

  6. #6
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Quote Originariamente inviata da fermat Visualizza il messaggio
    non so se ci sono altre soluzioni usando le annotations.
    Sì, ho fatto un esempio proprio ieri:
    http://forum.html.it/forum/showthrea...readid=2921276
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  7. #7
    ok seguendo i tuoi consigli ho risolto tutto.
    codice:
    @XmlRootElement(name = "book")
    public class Libro {
    
        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;
        }
    }
    i libri:
    codice:
    @XmlRootElement(name = "books")
    public class Libri {
    
        ArrayList<Libro> listaLibri;
    
        public void setListaLibri(ArrayList<Libro> book) {
            this.listaLibri = book;
        }
    
        @XmlElement(name = "book")
        public ArrayList<Libro> getListaLibri() {
            return listaLibri;
        }
    }
    creo la lista:
    codice:
    public class ExportXml {
        
        public void createXml(JTable table, File file) throws JAXBException {
            ArrayList<Libro> bookList = new ArrayList<>();
            for (int row = 0; row < table.getRowCount(); row++) {
                Libro bookModel = new Libro();
                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);
            }
            Libri books = new Libri();
            books.setListaLibri(bookList);
            JAXBContext context = JAXBContext.newInstance(Libri.class);
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.marshal(books, file);
        }
    }
    grazie per gli ottimi consigli!!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.