commit d91d6bd116037eb248f2674fa9bf770b898da0ad Author: michaelpaliz Date: Mon Nov 21 13:02:34 2022 +0100 Nuevo proyecto para el ejercicio diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..75f69ad --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..edb05c4 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..f8cce33 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.project b/.project new file mode 100644 index 0000000..cc3eaa8 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Ejercicio11 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/Ejercicio11.iml b/Ejercicio11.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Ejercicio11.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..22ac31b --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +

EJERCICIO 11

+

El ejercicio consiste en guardar objetos almacenados en un fichero .bin y trasladarlos o trasformarlos en un fichero xml

+

En mi caso creo una clase donde gestiona los tres basicos componentes para manipular el fichero.

+ + + +

Resultado

+ +```java +[Persona{id=1, nombre='Michael', edad=21} +, Persona{id=2, nombre='Jhoan', edad=19} +, Persona{id=3, nombre='Ricardo', edad=18} +] +``` diff --git a/out/production/Ejercicio11/Controlador/FuncionesXML.class b/out/production/Ejercicio11/Controlador/FuncionesXML.class new file mode 100644 index 0000000..6a08ead Binary files /dev/null and b/out/production/Ejercicio11/Controlador/FuncionesXML.class differ diff --git a/out/production/Ejercicio11/Main.class b/out/production/Ejercicio11/Main.class new file mode 100644 index 0000000..a05fd42 Binary files /dev/null and b/out/production/Ejercicio11/Main.class differ diff --git a/out/production/Ejercicio11/Modulo/Persona.class b/out/production/Ejercicio11/Modulo/Persona.class new file mode 100644 index 0000000..220a36c Binary files /dev/null and b/out/production/Ejercicio11/Modulo/Persona.class differ diff --git a/out/production/Ejercicio11/Modulo/Personas.class b/out/production/Ejercicio11/Modulo/Personas.class new file mode 100644 index 0000000..cb1c958 Binary files /dev/null and b/out/production/Ejercicio11/Modulo/Personas.class differ diff --git a/out/production/Ejercicio11/persona.bin b/out/production/Ejercicio11/persona.bin new file mode 100644 index 0000000..6704fd4 Binary files /dev/null and b/out/production/Ejercicio11/persona.bin differ diff --git a/persona.bin b/persona.bin new file mode 100644 index 0000000..5328f34 Binary files /dev/null and b/persona.bin differ diff --git a/personas.xml b/personas.xml new file mode 100644 index 0000000..641d04a --- /dev/null +++ b/personas.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/Controlador/FuncionesXML.java b/src/Controlador/FuncionesXML.java new file mode 100644 index 0000000..1f01f9f --- /dev/null +++ b/src/Controlador/FuncionesXML.java @@ -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 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); + } + + } + + + +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..5b32fb1 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,37 @@ +import Controlador.FuncionesXML; +import Modulo.Persona; +import Modulo.Personas; +import java.io.*; +import java.util.List; + +public class Main { + + private static Personas datos; + private static File fichero; + + + public static void main(String[] args) { + cargarDatos(); + FuncionesXML funcionesXML = new FuncionesXML(datos, fichero); + if (funcionesXML.escribirFichero(datos)){ + Personas personas = (Personas) funcionesXML.lecturaFichero(); + System.out.println(personas.toString()); + List listadoPersonas = personas.getPersonas(); + funcionesXML.crearXML(listadoPersonas,new File("personas.xml")); + }else{ + System.out.println("Error"); + } + + } + + + public static void cargarDatos(){ + fichero = new File("persona.bin"); + datos = new Personas(); + datos.add(new Persona("Michael", 21)); + datos.add(new Persona("Jhoan", 19)); + datos.add(new Persona("Ricardo", 18)); + } + + +} \ No newline at end of file diff --git a/src/Modulo/Persona.java b/src/Modulo/Persona.java new file mode 100644 index 0000000..658208a --- /dev/null +++ b/src/Modulo/Persona.java @@ -0,0 +1,38 @@ +package Modulo; + +import java.io.Serializable; + +public class Persona implements Serializable { + + private static int contador = 0; + private final int id; + private final String nombre; + private final int edad; + + public Persona(String nombre, int edad) { + this.id = ++contador; + this.nombre = nombre; + this.edad = edad; + } + + public int getId() { + return id; + } + + public String getNombre() { + return nombre; + } + + public int getEdad() { + return edad; + } + + @Override + public String toString() { + return "Persona{" + + "id=" + id + + ", nombre='" + nombre + '\'' + + ", edad=" + edad + + '}' + "\n"; + } +} diff --git a/src/Modulo/Personas.java b/src/Modulo/Personas.java new file mode 100644 index 0000000..0fe2f4d --- /dev/null +++ b/src/Modulo/Personas.java @@ -0,0 +1,34 @@ +package Modulo; + +import java.io.Serial; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +public class Personas implements Serializable { + + @Serial + private static final long serialVersionUID = 1L; + private final List personas; + + public Personas() { + personas = new ArrayList<>(); + } + + public void add(Persona persona){ + personas.add(persona); + } + + public List getPersonas() { + return personas; + } + + @Override + public String toString() { + return personas.toString(); + } + + + + +} diff --git a/src/persona.bin b/src/persona.bin new file mode 100644 index 0000000..6704fd4 Binary files /dev/null and b/src/persona.bin differ