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

    [Java] Creazione xml da JList

    ho un file xml.
    lo carico dentro a una jlist.
    poi posso aggingere/rimuovere elementi all'ArrayList.
    dopo le modifiche voglio salvare il tutto.
    ho provato a fare così.
    questo è il metodo per crear il file xml:
    codice:
        public static void save(ArrayList<ToDo> list) throws ParserConfigurationException, TransformerConfigurationException, TransformerException {
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.newDocument();
    
            Element todos = doc.createElement("todos");
            doc.appendChild(todos);
    
            Element todo = doc.createElement("todo");
            for (Object obj : list) {
                todo.appendChild(doc.createTextNode(obj.toString()));
            }
            todos.appendChild(todo);
    
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult("todo.xml");
            transformer.transform(source, result);
        }
    gli devo passare un arraylist prendendolo dal DefaultListModel del JList:
    codice:
                for (int i = 0; i < model.getSize(); i++) {
                    ArrayList<ToDo> newList =  model.getElementAt(i);
                }
                Xml.save(newList);
    ho ovviamente un problema di cast.
    come faccio a recuperare tutti i valori di JList e metterli dentro un array da passare poi al metodo??

  2. #2
    ho provato a fare così:
    codice:
    private DefaultListModel model = null;
    .........
        private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
            ArrayList<ToDo> newList = new ArrayList<ToDo>();
            try {
                for (int i = 0; i < model.getSize(); i++) {
                    newList.add((ToDo) model.getElementAt(i));
                }
                Xml.save(newList);
            } catch (ParserConfigurationException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            } catch (TransformerConfigurationException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            } catch (TransformerException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }
        }
    ottengo un:
    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to com.mattepuffo.todo.ToDo
    la classe ToDo è questa:
    codice:
    public class ToDo {
        private String todo;
    
        public ToDo(String todo) {
            this.todo = todo;
        }
    
        @Override
        public String toString() {
            return todo;
        }
    
        public void setTodo(String to) {
            to = this.todo;
        }
    }

  3. #3
    alla fine ho fatto così anche se sicuramente nn è il modo migliore:
    codice:
        public static void save(ArrayList<String> list) throws ParserConfigurationException, SAXException, IOException, TransformerConfigurationException, TransformerException {
            if (xmlFile.exists()) {
                xmlFile.delete();
            }
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.newDocument();
    
            Element todos = doc.createElement("todos");
            doc.appendChild(todos);
    
            for (Object obj : list) {
                Element todo = doc.createElement("todo");
                todo.appendChild(doc.createTextNode(obj.toString()));
                todos.appendChild(todo);
            }
    
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            if (!xmlFile.exists()) {
                xmlFile.createNewFile();
                StreamResult result = new StreamResult(xmlFile);
                transformer.transform(source, result);
            }
        }
    ho dovuto usare ArrayList<String> invece di ArrayList<ToDo>.
    ma cosa sicuro più importante nn sono riuscito ad aggiungere dinamicamente un nodo.
    il massimo che ho ottenuto è di aggiungere testo a un nodo, o di rimpiazzare tutti i nodi.

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.