public void EsportaXML() { // esporta su un file in formato XML int ret = jFileChooser.showSaveDialog(null); if (ret == JFileChooser.APPROVE_OPTION) { File file = jFileChooser.getSelectedFile(); try { JAXBContext context1 = JAXBContext.newInstance(ListaStudenti.class); Marshaller marshaller1 = context1.createMarshaller(); marshaller1.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller1.marshal(lista, file); marshaller1.marshal(lista, System.out); } catch(Exception ex) { JOptionPane.showMessageDialog(null, "errore esportazione file xml " + ex.getMessage(), "file xml", JOptionPane.ERROR_MESSAGE); } } }
public void ImportaXML() { // importa da file in formato XML int ret = jFileChooser.showOpenDialog(null); if (ret == JFileChooser.APPROVE_OPTION) { File file = jFileChooser.getSelectedFile(); try { JAXBContext context1 = JAXBContext.newInstance(ListaStudenti.class); Unmarshaller unmar = context1.createUnmarshaller(); lista = (ListaStudenti) unmar.unmarshal(file); lstStudenti.removeAll(); for(int i=0; i<lista.Dimensione(); i++) { lstStudenti.add(lista.Ottieni(i).toString()); } } catch(Exception ex) { JOptionPane.showMessageDialog(null, "errore importazione file xml " + ex.getMessage(), "file xml", JOptionPane.ERROR_MESSAGE); } } }