diff --git a/README.md b/README.md index 1b5b047..bdd13f8 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,53 @@ # Ejercicio_11 ### Introduccion -Escribe en un fichero ".xml" utilizando DOM +Escribe en un fichero ".xml" utilizando DOM y lee el fichero de objetos **Persona.dat** ### Documentacion +##### LeerFichero +~~~ +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.lang.reflect.Array; +import java.util.ArrayList; +~~~ + +##### Personas +~~~ +import java.io.Serializable; +import java.util.ArrayList; +~~~ +##### Persona +~~~ +import java.io.Serializable; +~~~ +##### ParserXml +~~~ +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +~~~ + +##### EscribirXml +~~~ +import java.io.File; +import java.util.ArrayList; + +import javax.xml.parsers.*; +import javax.xml.transform.*; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.w3c.dom.Attr; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +~~~ + + + ### Salida por pantalla ### Documentos generados +personas.xml diff --git a/bin/com/jorpelu/EscribirXML.class b/bin/com/jorpelu/EscribirXML.class new file mode 100644 index 0000000..975d529 Binary files /dev/null and b/bin/com/jorpelu/EscribirXML.class differ diff --git a/bin/com/jorpelu/LeerFichero.class b/bin/com/jorpelu/LeerFichero.class new file mode 100644 index 0000000..2a4ac31 Binary files /dev/null and b/bin/com/jorpelu/LeerFichero.class differ diff --git a/bin/com/jorpelu/ParserXml.class b/bin/com/jorpelu/ParserXml.class new file mode 100644 index 0000000..15b0988 Binary files /dev/null and b/bin/com/jorpelu/ParserXml.class differ diff --git a/bin/com/jorpelu/Persona.class b/bin/com/jorpelu/Persona.class index 87705ea..f463d31 100644 Binary files a/bin/com/jorpelu/Persona.class and b/bin/com/jorpelu/Persona.class differ diff --git a/bin/com/jorpelu/Personas.class b/bin/com/jorpelu/Personas.class new file mode 100644 index 0000000..7ca6fc6 Binary files /dev/null and b/bin/com/jorpelu/Personas.class differ diff --git a/bin/module-info.class b/bin/module-info.class index f4e845c..4e23e3c 100644 Binary files a/bin/module-info.class and b/bin/module-info.class differ diff --git a/persona2.dat b/persona2.dat new file mode 100644 index 0000000..97f67c8 Binary files /dev/null and b/persona2.dat differ diff --git a/personas.xml b/personas.xml new file mode 100644 index 0000000..0d8822c --- /dev/null +++ b/personas.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/com/jorpelu/EscribirXML.java b/src/com/jorpelu/EscribirXML.java new file mode 100644 index 0000000..ae0aba3 --- /dev/null +++ b/src/com/jorpelu/EscribirXML.java @@ -0,0 +1,47 @@ +package com.jorpelu; + +import java.io.File; +import java.util.ArrayList; + +import javax.xml.parsers.*; +import javax.xml.transform.*; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.w3c.dom.Attr; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +public class EscribirXML { + public EscribirXML(ArrayList personasList) { + try { + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = dbf.newDocumentBuilder(); + Document doc = db.newDocument(); + + Element eRaiz = doc.createElement("personas"); + doc.appendChild(eRaiz); + for(int i = 0; i personas; + + public LeerFichero(File fich) throws IOException, ClassNotFoundException { + //FileInputStream filein = new FileInputStream("C:\\Users\\jorge\\OneDrive\\Documentos\\ProyectosSPRING\\Ejercicio_11\\persona2.dat"); + personas = new ArrayList<>(); + FileInputStream filein = new FileInputStream(fich); + ObjectInputStream dataIS = new ObjectInputStream(filein); + + try { + while(true){ + Persona p1 = (Persona) dataIS.readObject(); + personas.add(p1); + } + } catch (EOFException e) { + System.out.println("Error leyendo el archivo en LeerFichero.java"); + e.printStackTrace(); + } + dataIS.close(); + } + public ArrayList getPersonas(){ + return personas; + } + +} diff --git a/src/com/jorpelu/ParserXml.java b/src/com/jorpelu/ParserXml.java new file mode 100644 index 0000000..69cb899 --- /dev/null +++ b/src/com/jorpelu/ParserXml.java @@ -0,0 +1,28 @@ +package com.jorpelu; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; + +public class ParserXml { + + public static void main(String[] args) throws ClassNotFoundException { + + File archivoDat = new File("./persona2.dat"); + LeerFichero ficheroLeido; + ArrayList personas = new ArrayList<>(); + + try { + ficheroLeido = new LeerFichero(archivoDat); + personas = ficheroLeido.getPersonas(); + EscribirXML escribirXML = new EscribirXML(personas); + + } catch (IOException e1) { + System.out.println("ErrorMain"); + e1.printStackTrace(); + } + + + } + +} diff --git a/src/com/jorpelu/Persona.java b/src/com/jorpelu/Persona.java index b105351..34d137b 100644 --- a/src/com/jorpelu/Persona.java +++ b/src/com/jorpelu/Persona.java @@ -1,5 +1,27 @@ package com.jorpelu; -public class Persona { +import java.io.Serializable; +public class Persona implements Serializable{ + private String nombre; + private int edad; + public Persona(String nombre, int edad) { + this.nombre = nombre; + this.edad= edad; + } + public Persona() { + this.nombre=null; + } + public String getNombre() { + return nombre; + } + public void setNombre(String nombre) { + this.nombre = nombre; + } + public int getEdad() { + return edad; + } + public void setEdad(int edad) { + this.edad = edad; + } } diff --git a/src/com/jorpelu/Personas.java b/src/com/jorpelu/Personas.java new file mode 100644 index 0000000..6ddb2fd --- /dev/null +++ b/src/com/jorpelu/Personas.java @@ -0,0 +1,23 @@ +package com.jorpelu; + +import java.io.Serializable; +import java.util.ArrayList; + +public class Personas implements Serializable{ + private ArrayList personas; + + public Personas() { + personas = new ArrayList<>(); + } + public Personas(ArrayList personas) { + this.personas = personas; + } + + public void AnyadirPersona(Persona p1) { + this.personas.add(p1); + } + + public ArrayList getPersonas(){ + return personas; + } +} diff --git a/src/module-info.java b/src/module-info.java index 3f16fe3..8902d0f 100644 --- a/src/module-info.java +++ b/src/module-info.java @@ -6,4 +6,5 @@ * */ module Ejercicio_11 { + requires java.xml; } \ No newline at end of file