|
@ -0,0 +1,134 @@ |
|
|
|
|
|
package creacionxml.com.daniminguet.es; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.EOFException; |
|
|
|
|
|
import java.io.File; |
|
|
|
|
|
import java.io.FileInputStream; |
|
|
|
|
|
import java.io.FileNotFoundException; |
|
|
|
|
|
import java.io.FileOutputStream; |
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
import java.io.ObjectInputStream; |
|
|
|
|
|
import java.io.ObjectOutputStream; |
|
|
|
|
|
import java.io.RandomAccessFile; |
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
|
|
|
|
import javax.xml.transform.Result; |
|
|
|
|
|
import javax.xml.parsers.DocumentBuilder; |
|
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory; |
|
|
|
|
|
import javax.xml.parsers.ParserConfigurationException; |
|
|
|
|
|
import javax.xml.transform.Source; |
|
|
|
|
|
import javax.xml.transform.Transformer; |
|
|
|
|
|
import javax.xml.transform.TransformerConfigurationException; |
|
|
|
|
|
import javax.xml.transform.TransformerException; |
|
|
|
|
|
import javax.xml.transform.TransformerFactory; |
|
|
|
|
|
import javax.xml.transform.TransformerFactoryConfigurationError; |
|
|
|
|
|
import javax.xml.transform.dom.DOMSource; |
|
|
|
|
|
import javax.xml.transform.stream.StreamResult; |
|
|
|
|
|
|
|
|
|
|
|
import org.w3c.dom.Attr; |
|
|
|
|
|
import org.w3c.dom.DOMImplementation; |
|
|
|
|
|
import org.w3c.dom.Document; |
|
|
|
|
|
import org.w3c.dom.Element; |
|
|
|
|
|
import org.w3c.dom.Node; |
|
|
|
|
|
import org.w3c.dom.NodeList; |
|
|
|
|
|
import org.w3c.dom.Text; |
|
|
|
|
|
|
|
|
|
|
|
public class P11 { |
|
|
|
|
|
public static void main(String[] args) throws EOFException { |
|
|
|
|
|
//Fichero de objetos Persona |
|
|
|
|
|
File fichero = new File("C:\\Users\\Dani\\OneDrive\\Documentos\\AccesoADatos\\Personas.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(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<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\\Dani\\OneDrive\\Documentos\\AccesoADatos\\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; |
|
|
|
|
|
//Lee los datos del archivo |
|
|
|
|
|
try { |
|
|
|
|
|
nombres = (String[]) dataIS.readObject(); |
|
|
|
|
|
edades = (int[]) dataIS.readObject(); |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
|
} catch (ClassNotFoundException e) { |
|
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//Se crea la lista que contendrá las personas |
|
|
|
|
|
ArrayList<Persona> personas = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
|
|
//Se recorren los datos de las personas y se crean y añaden en la lista |
|
|
|
|
|
for (int i = 0; i < nombres.length; i++) { |
|
|
|
|
|
personas.add(new Persona(nombres[i], edades[i])); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
dataIS.close(); |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return personas; |
|
|
|
|
|
} |
|
|
|
|
|
} |