|
|
@ -0,0 +1,96 @@ |
|
|
|
package Controlador; |
|
|
|
|
|
|
|
import Modulo.Persona; |
|
|
|
import Modulo.Personas; |
|
|
|
import org.w3c.dom.Document; |
|
|
|
import org.w3c.dom.Element; |
|
|
|
|
|
|
|
import javax.xml.parsers.DocumentBuilder; |
|
|
|
import javax.xml.parsers.DocumentBuilderFactory; |
|
|
|
import javax.xml.parsers.ParserConfigurationException; |
|
|
|
import javax.xml.transform.Transformer; |
|
|
|
import javax.xml.transform.TransformerException; |
|
|
|
import javax.xml.transform.TransformerFactory; |
|
|
|
import javax.xml.transform.dom.DOMSource; |
|
|
|
import javax.xml.transform.stream.StreamResult; |
|
|
|
import java.io.*; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
public class FuncionesXML { |
|
|
|
|
|
|
|
private final File fichero; |
|
|
|
private final Personas personas; |
|
|
|
|
|
|
|
public FuncionesXML(Personas personas, File fichero) { |
|
|
|
this.personas = personas; |
|
|
|
this.fichero = fichero; |
|
|
|
} |
|
|
|
|
|
|
|
public void crearXML(List<Persona> listadoPersonas, File nombreFicheroNuevo){ |
|
|
|
try { |
|
|
|
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); |
|
|
|
DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); |
|
|
|
|
|
|
|
//root elements |
|
|
|
Document doc = docBuilder.newDocument(); |
|
|
|
|
|
|
|
Element rootElement = doc.createElement("people"); |
|
|
|
doc.appendChild(rootElement); |
|
|
|
Element persona = doc.createElement("person"); |
|
|
|
|
|
|
|
for (Persona p : listadoPersonas) { |
|
|
|
rootElement.appendChild(persona); |
|
|
|
//set attribute to element |
|
|
|
persona.setAttribute("id", String.valueOf(p.getId())); |
|
|
|
persona.setAttribute("nombre", p.getNombre()); |
|
|
|
persona.setAttribute("edad", String.valueOf(p.getEdad())); |
|
|
|
} |
|
|
|
|
|
|
|
//write the content into xml file |
|
|
|
TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
|
|
|
Transformer transformer = transformerFactory.newTransformer(); |
|
|
|
DOMSource source = new DOMSource(doc); |
|
|
|
|
|
|
|
StreamResult result = new StreamResult(nombreFicheroNuevo); |
|
|
|
transformer.transform(source, result); |
|
|
|
|
|
|
|
System.out.println("Done!"); |
|
|
|
|
|
|
|
}catch(ParserConfigurationException | TransformerException pce){ |
|
|
|
pce.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Object lecturaFichero() { |
|
|
|
Object object = null; |
|
|
|
try (FileInputStream fileInputStream = new FileInputStream(fichero); |
|
|
|
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)){ |
|
|
|
object = objectInputStream.readObject(); |
|
|
|
} catch(IOException e){ |
|
|
|
e.printStackTrace(); |
|
|
|
} catch (ClassNotFoundException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
return object; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public boolean escribirFichero(Object obj){ |
|
|
|
try (FileOutputStream fileOutputStream = new FileOutputStream(fichero); |
|
|
|
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream) |
|
|
|
){ |
|
|
|
objectOutputStream.writeObject(obj); |
|
|
|
objectOutputStream.flush(); |
|
|
|
return true; |
|
|
|
} catch (IOException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |