|
|
@ -0,0 +1,58 @@ |
|
|
|
package com.antoniofrische; |
|
|
|
|
|
|
|
import org.w3c.dom.*; |
|
|
|
import javax.xml.parsers.*; |
|
|
|
import javax.xml.transform.*; |
|
|
|
import javax.xml.transform.dom.*; |
|
|
|
import javax.xml.transform.stream.*; |
|
|
|
import java.io.*; |
|
|
|
|
|
|
|
public class XmlDomParser { |
|
|
|
public static void main(String[] args) throws IOException{ |
|
|
|
File f = new File("C:\\Users\\AntonioFrische\\OneDrive - ABACCO Solutions\\Documents\\Schule_22_23_CFGS\\Acceso_Datos\\com.antoniofrische.Persona.dat"); |
|
|
|
RandomAccessFile raf = new RandomAccessFile(f, "r"); |
|
|
|
Persona[] personas; |
|
|
|
try { |
|
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
|
|
|
DocumentBuilder builder = factory.newDocumentBuilder(); |
|
|
|
DOMImplementation implementation = builder.getDOMImplementation(); |
|
|
|
Document document = implementation.createDocument(null, "Personas", null); |
|
|
|
document.setXmlVersion("1.0"); |
|
|
|
SeriableParser parser = new SeriableParser(f); |
|
|
|
personas = parser.getPersonas(); |
|
|
|
System.out.println(personas.length); |
|
|
|
for(int i = 0; i < personas.length; i++) { |
|
|
|
|
|
|
|
System.out.println("Edad: " + personas[i].getEdad() + " NOmbre: " + personas[i].getNombre()); |
|
|
|
Element raiz = document.createElement("Personas"); |
|
|
|
document.getDocumentElement().appendChild(raiz); |
|
|
|
crearElemento("Edad",Integer.toString(personas[i].getEdad()),raiz,document); // añadir Edad |
|
|
|
crearElemento("Nombre",personas[i].getNombre(),raiz,document); // añadir nombre |
|
|
|
|
|
|
|
if (raf.getFilePointer() >= raf.length()) break; |
|
|
|
} |
|
|
|
Source source = new DOMSource(document); |
|
|
|
Result result = new StreamResult (new File("C:\\Users\\AntonioFrische\\OneDrive - ABACCO Solutions\\Documents\\Schule_22_23_CFGS\\Acceso_Datos\\Personas.xml")); |
|
|
|
Transformer transformer = TransformerFactory.newInstance().newTransformer(); |
|
|
|
transformer.transform(source,result); |
|
|
|
}catch(EOFException eofe) { |
|
|
|
System.out.println("Error End Of File: " + eofe.getMessage()); |
|
|
|
}catch(IOException e) { |
|
|
|
System.out.println("Error IO: " + e.getMessage()); |
|
|
|
}catch(TransformerConfigurationException tce) { |
|
|
|
System.out.println("Error Transformer Configuration: " + tce.getMessage()); |
|
|
|
}catch(TransformerException te) { |
|
|
|
System.out.println("Error Transformer: " + te.getMessage()); |
|
|
|
}catch(ParserConfigurationException pce) { |
|
|
|
System.out.println("Error Parser Configuration: " + pce.getMessage()); |
|
|
|
}finally { |
|
|
|
raf.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
static void crearElemento(String datoEmpleado, String valor, Element raiz, Document document) { |
|
|
|
Element elem = document.createElement (datoEmpleado); //creamos hijo |
|
|
|
Text text = document.createTextNode(valor); // damos valor al campo |
|
|
|
raiz.appendChild(elem); //pegamos el elemento hijo a la raiz |
|
|
|
elem.appendChild(text); //pegamos el valor |
|
|
|
} |
|
|
|
} |