|
@ -0,0 +1,116 @@ |
|
|
|
|
|
package dam.com.es; |
|
|
|
|
|
|
|
|
|
|
|
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 { |
|
|
|
|
|
//Fichero de objetos Persona |
|
|
|
|
|
File fichero = new File("C:\\Users\\kevin\\Documents\\Atividad11\\FichPersona.dat"); |
|
|
|
|
|
//Guardamos los datos de las personas para poder crear el archivo XML |
|
|
|
|
|
ArrayList<Persona> personas = leerFichero(fichero); |
|
|
|
|
|
//Comprueba que el archivo XML se ha creado correctamente |
|
|
|
|
|
if(crearArchivoXml(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 crearArchivoXml(ArrayList<Persona> personas) { |
|
|
|
|
|
boolean creadoCorrectamente = false; |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); |
|
|
|
|
|
DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); |
|
|
|
|
|
Document doc = docBuilder.newDocument(); |
|
|
|
|
|
|
|
|
|
|
|
//Se define el array de personas |
|
|
|
|
|
Element personasXml = doc.createElement("Personas"); |
|
|
|
|
|
doc.appendChild(personasXml); |
|
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < personas.size(); i++) { |
|
|
|
|
|
//Se definen las personas |
|
|
|
|
|
Element persona = doc.createElement("Persona"); |
|
|
|
|
|
personasXml.appendChild(persona); |
|
|
|
|
|
//Se definen los atributos de cada persona, se les asigna el valor adecuado y se añaden al elemento persona |
|
|
|
|
|
Attr nombre = doc.createAttribute("nombre"); |
|
|
|
|
|
nombre.setValue(personas.get(i).getNombre()); |
|
|
|
|
|
persona.setAttributeNode(nombre); |
|
|
|
|
|
Attr edad = doc.createAttribute("edad"); |
|
|
|
|
|
edad.setValue(String.valueOf(personas.get(i).getEdad())); |
|
|
|
|
|
persona.setAttributeNode(edad); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//Se escribe el contenido del XML en un archivo |
|
|
|
|
|
TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
|
|
|
|
|
Transformer transformer = transformerFactory.newTransformer(); |
|
|
|
|
|
DOMSource source = new DOMSource(doc); |
|
|
|
|
|
StreamResult result = new StreamResult(new File("C:\\Users\\kevin\\Documents\\Atividad11\\Personas.xml")); |
|
|
|
|
|
transformer.transform(source, result); |
|
|
|
|
|
|
|
|
|
|
|
creadoCorrectamente = true; |
|
|
|
|
|
} catch (ParserConfigurationException pce) { |
|
|
|
|
|
pce.printStackTrace(); |
|
|
|
|
|
} catch (TransformerException tfe) { |
|
|
|
|
|
tfe.printStackTrace(); |
|
|
|
|
|
} |
|
|
|
|
|
return creadoCorrectamente; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static ArrayList<Persona> 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; |
|
|
|
|
|
int[] edades; |
|
|
|
|
|
|
|
|
|
|
|
try {//Qui es donde lee los datos del archivo |
|
|
|
|
|
nombres = (String[]) dataIS.readObject(); |
|
|
|
|
|
edades = (int[]) dataIS.readObject(); |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
|
} catch (ClassNotFoundException e) { |
|
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
|
} |
|
|
|
|
|
ArrayList<Persona> personas = new ArrayList<>(); //Se crea la lista que tendra los datos de las personas |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < nombres.length; i++) {//Cojemos los datos y se crea nueva paginas y añaden en la lista de personas |
|
|
|
|
|
personas.add(new Persona(nombres[i], edades[i])); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
dataIS.close(); |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return personas; |
|
|
|
|
|
} |
|
|
|
|
|
} |