| @ -1,9 +1,53 @@ | |||||
| # Ejercicio_11 | # Ejercicio_11 | ||||
| ### Introduccion | ### Introduccion | ||||
| Escribe en un fichero ".xml" utilizando DOM | |||||
| Escribe en un fichero ".xml" utilizando DOM y lee el fichero de objetos **Persona.dat** | |||||
| ### Documentacion | ### 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 | ### Salida por pantalla | ||||
| ### Documentos generados | ### Documentos generados | ||||
| personas.xml | |||||
| @ -0,0 +1 @@ | |||||
| <?xml version="1.0" encoding="UTF-8" standalone="no"?><personas/> | |||||
| @ -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<Persona> 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<personasList.size(); i++) { | |||||
| Element ePersona = doc.createElement("persona"); | |||||
| eRaiz.appendChild(ePersona); | |||||
| Element eNombre = doc.createElement("nombre"); | |||||
| eNombre.appendChild(doc.createTextNode(personasList.get(i).getNombre())); | |||||
| ePersona.appendChild(eNombre); | |||||
| Element eEdad = doc.createElement("edad"); | |||||
| eEdad.appendChild(doc.createTextNode(String.valueOf(personasList.get(i).getEdad()))); | |||||
| ePersona.appendChild(eEdad); | |||||
| } | |||||
| TransformerFactory transformerFactory = TransformerFactory.newInstance(); | |||||
| Transformer transformer = transformerFactory.newTransformer(); | |||||
| DOMSource source = new DOMSource(doc); | |||||
| StreamResult result = new StreamResult(new File("personas.xml")); | |||||
| transformer.transform(source, result); | |||||
| }catch (Exception e) { | |||||
| System.out.println("Error al escribir el .xml"); | |||||
| e.getLocalizedMessage(); | |||||
| } | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,34 @@ | |||||
| package com.jorpelu; | |||||
| import java.io.EOFException; | |||||
| import java.io.File; | |||||
| import java.io.FileInputStream; | |||||
| import java.io.IOException; | |||||
| import java.io.ObjectInputStream; | |||||
| import java.util.ArrayList; | |||||
| public class LeerFichero { | |||||
| private ArrayList<Persona> 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<Persona> getPersonas(){ | |||||
| return personas; | |||||
| } | |||||
| } | |||||
| @ -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<Persona> 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(); | |||||
| } | |||||
| } | |||||
| } | |||||
| @ -1,5 +1,27 @@ | |||||
| package com.jorpelu; | 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; | |||||
| } | |||||
| } | } | ||||
| @ -0,0 +1,23 @@ | |||||
| package com.jorpelu; | |||||
| import java.io.Serializable; | |||||
| import java.util.ArrayList; | |||||
| public class Personas implements Serializable{ | |||||
| private ArrayList<Persona> personas; | |||||
| public Personas() { | |||||
| personas = new ArrayList<>(); | |||||
| } | |||||
| public Personas(ArrayList<Persona> personas) { | |||||
| this.personas = personas; | |||||
| } | |||||
| public void AnyadirPersona(Persona p1) { | |||||
| this.personas.add(p1); | |||||
| } | |||||
| public ArrayList<Persona> getPersonas(){ | |||||
| return personas; | |||||
| } | |||||
| } | |||||