|
@ -0,0 +1,125 @@ |
|
|
|
|
|
package com.cristobalbernal.Actividad11; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.EOFException; |
|
|
|
|
|
import java.io.File; |
|
|
|
|
|
import java.io.FileInputStream; |
|
|
|
|
|
import java.io.FileNotFoundException; |
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
import java.io.ObjectInputStream; |
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
|
|
|
|
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 org.w3c.dom.Attr; |
|
|
|
|
|
import org.w3c.dom.Document; |
|
|
|
|
|
import org.w3c.dom.Element; |
|
|
|
|
|
|
|
|
|
|
|
public class Main { |
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws EOFException { |
|
|
|
|
|
File fichero = new File("C:\\Users\\crist\\Documents\\2DAM\\BaseDeDatos\\Personas.dat"); |
|
|
|
|
|
ArrayList<Personas> personas = leerFichero(fichero); |
|
|
|
|
|
|
|
|
|
|
|
if(crearXml(personas)) { |
|
|
|
|
|
System.out.println("El archivo XML de personas se ha creado correctamente!"); |
|
|
|
|
|
} else { |
|
|
|
|
|
System.out.println("Error al crear el archivo XML de personas"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static boolean crearXml(ArrayList<Personas> personas) { |
|
|
|
|
|
boolean creadoCorrectamente = false; |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); |
|
|
|
|
|
DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); |
|
|
|
|
|
Document doc = docBuilder.newDocument(); |
|
|
|
|
|
|
|
|
|
|
|
Element personasXml = doc.createElement("Personas"); |
|
|
|
|
|
doc.appendChild(personasXml); |
|
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < personas.size(); i++) { |
|
|
|
|
|
Element persona = doc.createElement("Persona"); |
|
|
|
|
|
personasXml.appendChild(persona); |
|
|
|
|
|
|
|
|
|
|
|
Attr nombre = doc.createAttribute("nombre"); |
|
|
|
|
|
nombre.setValue(personas.get(i).getNombre()); |
|
|
|
|
|
persona.setAttributeNode(nombre); |
|
|
|
|
|
Attr apellido = doc.createAttribute("apellido"); |
|
|
|
|
|
apellido.setValue(personas.get(i).getApellido()); |
|
|
|
|
|
persona.setAttributeNode(apellido); |
|
|
|
|
|
Attr edad = doc.createAttribute("edad"); |
|
|
|
|
|
edad.setValue(String.valueOf(personas.get(i).getEdad())); |
|
|
|
|
|
persona.setAttributeNode(edad); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
|
|
|
|
|
Transformer transformer = transformerFactory.newTransformer(); |
|
|
|
|
|
DOMSource source = new DOMSource(doc); |
|
|
|
|
|
StreamResult result = new StreamResult(new File("C:\\Users\\crist\\Documents\\2DAM\\BaseDeDatos\\Personas.xml")); |
|
|
|
|
|
transformer.transform(source, result); |
|
|
|
|
|
|
|
|
|
|
|
creadoCorrectamente = true; |
|
|
|
|
|
} catch (ParserConfigurationException pce) { |
|
|
|
|
|
pce.printStackTrace(); |
|
|
|
|
|
} catch (TransformerException tfe) { |
|
|
|
|
|
tfe.printStackTrace(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return creadoCorrectamente; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("resource") |
|
|
|
|
|
public static ArrayList<Personas> leerFichero(File fichero) { |
|
|
|
|
|
FileInputStream filein = null; |
|
|
|
|
|
try { |
|
|
|
|
|
filein = new FileInputStream(fichero); |
|
|
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
|
} |
|
|
|
|
|
ObjectInputStream dataIS = null; |
|
|
|
|
|
try { |
|
|
|
|
|
dataIS = new ObjectInputStream(filein); |
|
|
|
|
|
} catch (IOException ioe) { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String[] nombres; |
|
|
|
|
|
String[] apellidos; |
|
|
|
|
|
int[] edades; |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
nombres = (String[]) dataIS.readObject(); |
|
|
|
|
|
apellidos = (String[]) dataIS.readObject(); |
|
|
|
|
|
edades = (int[]) dataIS.readObject(); |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
|
} catch (ClassNotFoundException e) { |
|
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ArrayList<Personas> personas = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < nombres.length; i++) { |
|
|
|
|
|
personas.add(new Personas(nombres[i],apellidos[i],edades[i])); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
dataIS.close(); |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return personas; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |