From dc50a875a121b535b35dcfb8df8bf7c452175ee0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isidoro=20Nevares=20Mart=C3=ADn?= Date: Tue, 27 Oct 2026 13:44:28 +0100 Subject: [PATCH] Lectura XML y almacenamiento XML-TXT --- config/application.properties | 7 +- src/org/lapaloma/aadd/inm/AppPrincipal.java | 344 ++++++++++++++---- .../inm/gestores/GestorConfiguracion.java | 31 +- 3 files changed, 306 insertions(+), 76 deletions(-) diff --git a/config/application.properties b/config/application.properties index 701424a..787e115 100644 --- a/config/application.properties +++ b/config/application.properties @@ -2,4 +2,9 @@ # y que no se repitan con otras entradas de la misma aplicación # Información relativa al IES -ies.ruta.fichero=C:\\Users\\Isidoro NM\\03-AADD-2AMT\\ficheros\\act1_11\\informacion-IES.xml +entrada.ies.ruta.fichero=C:\\Users\\ineva\\INM\\tmp\\aadd\\fic12\\informacion-IES_v2.xml + +salida.ies.ruta.fichero=C:\\Users\\ineva\\INM\\tmp\\aadd\\fic12\\info_IES.xml +salida.ciclos.ruta.fichero=C:\\Users\\ineva\\INM\\tmp\\aadd\\fic12\\resumen_ciclos_IES.xml +salida.modulos.ruta.fichero=C:\\Users\\ineva\\INM\\tmp\\aadd\\fic12\\modulos1_ciclo_DAM.xml +salida.informe.ruta.fichero=C:\\Users\\ineva\\INM\\tmp\\aadd\\fic12\\informe_ciclos_IES.txt diff --git a/src/org/lapaloma/aadd/inm/AppPrincipal.java b/src/org/lapaloma/aadd/inm/AppPrincipal.java index febe275..a36ba89 100644 --- a/src/org/lapaloma/aadd/inm/AppPrincipal.java +++ b/src/org/lapaloma/aadd/inm/AppPrincipal.java @@ -1,113 +1,323 @@ package org.lapaloma.aadd.inm; +import java.io.BufferedWriter; import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import java.util.Scanner; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +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.palomafp.aadd.inm.gestores.GestorConfiguracion; import org.w3c.dom.Document; import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; public class AppPrincipal { + private static final String CICLO_DAM = "DAM"; + private static final String CURSO1 = "1"; + public static void main(String[] args) { AppPrincipal app = new AppPrincipal(); - // Se carga el Ć”rbol DOM que mĆ”s tarde se usarĆ” para las consultas. - Element elementoRaizArbolDOM = app.cargarArbolDOMFicheroXML(); - + // Se carga el Ć”rbol DOM que mĆ”s tarde se usarĆ” para las consultas. + Document docArbolDOM = app.obtenerArbolDOMFicheroXML(); + Scanner sc = new Scanner(System.in); - int opcion; + int opcion; - do { - System.out.println("Elige una opción:"); - System.out.println("1. Buscar información instituto"); - System.out.println("2. Buscar información ciclos"); - System.out.println("3. Buscar información módulos - filtro 1"); - System.out.println("4. Buscar información módulos - filtro 2"); + do { + System.out.println("Elige una opción:"); + System.out.println("1. Crear XML información instituto"); + System.out.println("2. Crear XML resumen ciclos"); + System.out.println("3. Crear XML módulos 1Āŗ - Ciclo DAM"); + System.out.println("4. Crear informe TXT Ciclos"); - opcion = sc.nextInt(); + opcion = sc.nextInt(); - if (opcion < 1 || opcion > 4) { - System.out.println("Opción no vĆ”lida. IntĆ©ntalo de nuevo."); - } - switch (opcion) { - case 1: - app.buscarInformacionInstituto(elementoRaizArbolDOM); - break; - case 2: - app.buscarInformacionCiclos(elementoRaizArbolDOM); - break; - case 3: - app.buscarInformacionModulos(elementoRaizArbolDOM); - break; - case 4: - app.buscarInformacionModulosCiclo(elementoRaizArbolDOM); - break; - } + if (opcion < 1 || opcion > 4) { + System.out.println("Opción no vĆ”lida. IntĆ©ntalo de nuevo."); + } + switch (opcion) { + case 1: + app.crearXMLInformacionInstituto(docArbolDOM); + break; + case 2: + app.crearXMLInformacionCiclos(docArbolDOM); + break; + case 3: + app.crearXMLInformacionModulos(docArbolDOM); + break; + case 4: + app.crearInformeTXTCiclos(docArbolDOM); + break; + } - } while (opcion < 1 || opcion > 4); + } while (opcion < 1 || opcion > 4); - System.out.println("Has elegido la opción " + opcion); + System.out.println("Has elegido la opción " + opcion); - sc.close(); + sc.close(); } - private void buscarInformacionInstituto(Element elementoRaizArbolDOM) { - Element elementoInstituto = (Element) elementoRaizArbolDOM - .getElementsByTagName("instituto") - .item(0); + private void crearXMLInformacionInstituto(Document docArbolDOM) { + // Se crea un nuevo documento XML para almacenar la información del instituto + Document docNuevoXMLIES = crearDocumentoXML(); - String nombre = elementoInstituto.getAttribute("nombre"); - String tipo = elementoInstituto.getAttribute("tipo"); + // Se crea el elemento raĆ­z "instituto" y se aƱade al nuevo documento + Element elementoIES = docNuevoXMLIES.createElement("instituto"); + docNuevoXMLIES.appendChild(elementoIES); - System.out.println("Nombre: " + nombre + " - " + tipo); - - + // Se crea el elemento "nombre", se obtiene su valor del documento original y se + // aƱade al elemento raĆ­z + Element elementoNombreIES = docNuevoXMLIES.createElement("nombre"); + String nombreIES = docArbolDOM.getElementsByTagName("nombre").item(0).getTextContent(); + elementoNombreIES.setTextContent(nombreIES); + + // Se aƱade el elemento "nombre" al elemento raĆ­z "instituto" + elementoIES.appendChild(elementoNombreIES); + + guardarDocumentoXMLFichero(docNuevoXMLIES, GestorConfiguracion.getRutaFicheroSalidaIES()); } - private void buscarInformacionModulosCiclo(Element elementoRaizArbolDOM) { - // TODO Auto-generated method stub - + private void crearXMLInformacionCiclos(Document docArbolDOM) { + // Se crea un nuevo documento XML para almacenar la información del instituto + Document docNuevoXMLIES = crearDocumentoXML(); + + // Se crea el elemento raĆ­z "resumen" y se aƱade al nuevo documento + Element elementoResumen = docNuevoXMLIES.createElement("resumen"); + docNuevoXMLIES.appendChild(elementoResumen); + + // Se obtiene los valores de instituto del documento original + Element elementoInstitutoOrigen = (Element) docArbolDOM.getElementsByTagName("instituto").item(0); + String nombre = elementoInstitutoOrigen.getElementsByTagName("nombre").item(0).getTextContent(); + String tipo = elementoInstitutoOrigen.getAttribute("tipo"); + // Se crea el elemento "instituto" y se aƱade al elemento raĆ­z "resumen" + Element elementoIES = docNuevoXMLIES.createElement("instituto"); + elementoIES.setTextContent(nombre); + elementoIES.setAttribute("tipo", tipo); + + // Se aƱade el elemento "nombre" al elemento raĆ­z "instituto" + elementoResumen.appendChild(elementoIES); + + Element elementoCiclos = docNuevoXMLIES.createElement("ciclos"); + elementoResumen.appendChild(elementoCiclos); + + // Se obtiene los valores de ciclos del documento original + NodeList nodosCiclo = docArbolDOM.getElementsByTagName("ciclo"); + for (int i = 0; i < nodosCiclo.getLength(); i++) { + Element elementoCicloOrigen = (Element) nodosCiclo.item(i); + + String codigo = elementoCicloOrigen.getAttribute("codigo"); + String nombreCorto = elementoCicloOrigen.getElementsByTagName("nombre_corto").item(0).getTextContent(); + + // Se crea el elemento "ciclo" y se aƱade al elemento "ciclos" + Element elementoCicloNuevo = docNuevoXMLIES.createElement("ciclo"); + elementoCicloNuevo.setAttribute("codigo", codigo); + elementoCicloNuevo.setTextContent(nombreCorto); + + elementoCiclos.appendChild(elementoCicloNuevo); + } + + guardarDocumentoXMLFichero(docNuevoXMLIES, GestorConfiguracion.getRutaFicheroSalidaCiclos()); } - private void buscarInformacionModulos(Element elementoRaizArbolDOM) { - // TODO Auto-generated method stub - + private void crearXMLInformacionModulos(Document docArbolDOM) { + // Se crea un nuevo documento XML para almacenar la información del instituto + Document docNuevoXMLIES = crearDocumentoXML(); + + // Se crea el elemento raĆ­z "instituto" y se aƱade al nuevo documento + Element elementoIES = docNuevoXMLIES.createElement("ies"); + docNuevoXMLIES.appendChild(elementoIES); + + // Se obtiene los valores de instituto del documento original + Element elementoInstitutoOrigen = (Element) docArbolDOM.getElementsByTagName("instituto").item(0); + String nombre = elementoInstitutoOrigen.getElementsByTagName("nombre").item(0).getTextContent(); + String tipo = elementoInstitutoOrigen.getAttribute("tipo"); + // Se crea el elemento "instituto" y se aƱade al elemento raĆ­z "resumen" + elementoIES.setAttribute("nombre", nombre); + elementoIES.setAttribute("tipo", tipo); + + // Se obtiene los valores de ciclos del documento original + NodeList nodosCiclo = docArbolDOM.getElementsByTagName("ciclo"); + for (int i = 0; i < nodosCiclo.getLength(); i++) { + Element elementoCicloOrigen = (Element) nodosCiclo.item(i); + + String codigo = elementoCicloOrigen.getAttribute("codigo"); + String nombreCorto = elementoCicloOrigen.getElementsByTagName("nombre_corto").item(0).getTextContent(); + String nombreLargo = elementoCicloOrigen.getElementsByTagName("nombre_largo").item(0).getTextContent(); + + // Saltar al siguiente ciclo si el código no es "DAM" + if (!CICLO_DAM.equals(nombreCorto.trim())) { + continue; + } + + // Se crea el elemento "ciclo" y se aƱade al elemento "ciclos" + Element elementoCicloNuevo = docNuevoXMLIES.createElement("ciclo"); + elementoCicloNuevo.setAttribute("codigo", codigo); + + Element elementoNombreCortoCicloNuevo = docNuevoXMLIES.createElement("nombre_corto"); + elementoNombreCortoCicloNuevo.setTextContent(nombreCorto); + elementoCicloNuevo.appendChild(elementoNombreCortoCicloNuevo); + + Element elementoNombreLargoCicloNuevo = docNuevoXMLIES.createElement("nombre_largo"); + elementoNombreLargoCicloNuevo.setTextContent(nombreLargo); + elementoCicloNuevo.appendChild(elementoNombreLargoCicloNuevo); + + elementoIES.appendChild(elementoCicloNuevo); + + NodeList nodosCurso = elementoCicloOrigen.getElementsByTagName("curso"); + for (int j = 0; j < nodosCurso.getLength(); j++) { + Element elementoCursoOrigen = (Element) nodosCurso.item(j); + String numeroCurso = elementoCursoOrigen.getAttribute("numero"); + + // Saltar al siguiente curso si el nĆŗmero no es "1" + if (!CURSO1.equals(numeroCurso.trim())) { + continue; + } + // Se crea el elemento "curso" y se aƱade al elemento "ciclo" + Element elementoCursoCicloNuevo = docNuevoXMLIES.createElement("curso"); + elementoCursoCicloNuevo.setAttribute("numero", numeroCurso); + elementoCicloNuevo.appendChild(elementoCursoCicloNuevo); + + NodeList nodosModulo = elementoCursoOrigen.getElementsByTagName("modulo"); + for (int k = 0; k < nodosModulo.getLength(); k++) { + Element elementoModuloOrigen = (Element) nodosModulo.item(k); + String id = elementoModuloOrigen.getAttribute("id"); + String ref = elementoModuloOrigen.getAttribute("ref"); + String nombreModulo = elementoModuloOrigen.getElementsByTagName("titulo").item(0).getTextContent(); + String numeroHoras = elementoModuloOrigen.getElementsByTagName("numerohoras").item(0) + .getTextContent(); + + // Se crea el elemento "modulo" y se aƱade al elemento "curso" + Element elementoModuloNuevo = docNuevoXMLIES.createElement("modulo"); + elementoModuloNuevo.setAttribute("id", id); + elementoModuloNuevo.setAttribute("ref", ref); + elementoModuloNuevo.setAttribute("numero_horas", numeroHoras); + Element elementoTituloModuloNuevo = docNuevoXMLIES.createElement("titulo"); + elementoTituloModuloNuevo.setTextContent(nombreModulo); + elementoModuloNuevo.appendChild(elementoTituloModuloNuevo); + + // Se aƱade el elemento "modulo" al elemento "curso" + elementoCursoCicloNuevo.appendChild(elementoModuloNuevo); + } + } + } + + guardarDocumentoXMLFichero(docNuevoXMLIES, GestorConfiguracion.getRutaFicheroSalidaModulos()); + } - private void buscarInformacionCiclos(Element elementoRaizArbolDOM) { - // TODO Auto-generated method stub - + private void crearInformeTXTCiclos(Document docArbolDOM) { + File ficheroInforme = new File(GestorConfiguracion.getRutaFicheroInformeCiclos()); + try (BufferedWriter bw = new BufferedWriter(new FileWriter(ficheroInforme))) { + + bw.write("INFORME DE CICLOS FORMATIVOS"); + bw.newLine(); + bw.write("IES Virgen de la Paloma"); + bw.newLine(); + bw.write("========================================"); + bw.newLine(); + bw.newLine(); + // Obtener los ciclos del documento original + NodeList nodosCiclo = docArbolDOM.getElementsByTagName("ciclo"); + for (int i = 0; i < nodosCiclo.getLength(); i++) { + Element elementoCiclo = (Element) nodosCiclo.item(i); + String codigo = elementoCiclo.getAttribute("codigo"); + String nombreCorto = elementoCiclo.getElementsByTagName("nombre_corto").item(0).getTextContent(); + + bw.write("Código: " + codigo); + bw.newLine(); + bw.write("Nombre corto: " + nombreCorto); + bw.newLine(); + bw.write("Obervaciones:"); + bw.newLine(); + // Se obtienen los nodos hijos del ciclo para buscar los comentarios. + NodeList nodosHijos = elementoCiclo.getChildNodes(); + for (int j = 0; j < nodosHijos.getLength(); j++) { + Node nodoHijoCiclo = nodosHijos.item(j); + if (nodoHijoCiclo.getNodeType() == Node.COMMENT_NODE) { + String obsevacion = nodoHijoCiclo.getNodeValue(); + bw.write("- " + obsevacion); + bw.newLine(); + bw.newLine(); + bw.write("----------------------------------------"); + bw.newLine(); + break; + } + } + } + bw.newLine(); + bw.write("Fin del informe"); + } catch (IOException e) { + throw new RuntimeException("Error al guardar el fichero de texto", e); + } } - - private Element cargarArbolDOMFicheroXML() { - Element raiz = null; + private Document obtenerArbolDOMFicheroXML() { + Document documentoRaiz = null; try { - // 1. Crear la fĆ”brica - DocumentBuilderFactory factory = - DocumentBuilderFactory.newInstance(); + // 1. Crear la fĆ”brica + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - // 2. Crear el parser DOM - DocumentBuilder builder = - factory.newDocumentBuilder(); + // 2. Crear el parser DOM + DocumentBuilder builder = factory.newDocumentBuilder(); - // 3. Leer el fichero XML - Document document = - builder.parse(new File(GestorConfiguracion.getRutaFicheroIES())); + // 3. Leer el fichero XML + documentoRaiz = builder.parse(new File(GestorConfiguracion.getRutaFicheroEntradaIES())); - // 4. Obtener el elemento raĆ­z que voy a devolver - document.getDocumentElement().normalize(); - - raiz = document.getDocumentElement(); - System.out.println("RaĆ­z: " + raiz.getTagName()); + // 4. Obtener el elemento raĆ­z que voy a devolver + documentoRaiz.getDocumentElement().normalize(); } catch (Exception e) { e.printStackTrace(); } - return raiz; + return documentoRaiz; + } + + private Document crearDocumentoXML() { + try { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + + DocumentBuilder builder = factory.newDocumentBuilder(); + + return builder.newDocument(); + + } catch (ParserConfigurationException e) { + throw new RuntimeException("Error al crear el documento XML", e); + } + } + + private void guardarDocumentoXMLFichero(Document doc, String ruta) { + try { + + TransformerFactory factory = TransformerFactory.newInstance(); + Transformer transformer = factory.newTransformer(); + + // Configurar la salida XML + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); + transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); + + // Transformar el DOM en un fichero + DOMSource origen = new DOMSource(doc); + StreamResult destino = new StreamResult(new File(ruta)); + + transformer.transform(origen, destino); + } catch (TransformerException e) { + e.printStackTrace(); // Mostrar la causa original + + throw new RuntimeException("Error al transforma el documento XML en fichero", e); + } } } diff --git a/src/org/palomafp/aadd/inm/gestores/GestorConfiguracion.java b/src/org/palomafp/aadd/inm/gestores/GestorConfiguracion.java index 224535f..c36b52b 100644 --- a/src/org/palomafp/aadd/inm/gestores/GestorConfiguracion.java +++ b/src/org/palomafp/aadd/inm/gestores/GestorConfiguracion.java @@ -27,13 +27,12 @@ public class GestorConfiguracion { // Este bloque se ejecuta una sola vez, cuando se carga la clase // GestorConfiguracion static { - try (InputStream inputStream = GestorConfiguracion.class.getClassLoader().getResourceAsStream(RUTA_FICHERO_CONFIGURACION);) - { + try (InputStream inputStream = GestorConfiguracion.class.getClassLoader() + .getResourceAsStream(RUTA_FICHERO_CONFIGURACION);) { - if (inputStream == null) { - throw new IOException( - "No se encuentra "+ RUTA_FICHERO_CONFIGURACION + " en el classpath"); - } + if (inputStream == null) { + throw new IOException("No se encuentra " + RUTA_FICHERO_CONFIGURACION + " en el classpath"); + } PROPIEDADES_CONFIGURACION.load(inputStream); // System.out.println(PROPIEDADES_CONFIGURACION); } catch (IOException e) { @@ -41,7 +40,23 @@ public class GestorConfiguracion { } } - public static String getRutaFicheroIES() { - return PROPIEDADES_CONFIGURACION.getProperty("ies.ruta.fichero"); + public static String getRutaFicheroEntradaIES() { + return PROPIEDADES_CONFIGURACION.getProperty("entrada.ies.ruta.fichero"); + } + + public static String getRutaFicheroSalidaIES() { + return PROPIEDADES_CONFIGURACION.getProperty("salida.ies.ruta.fichero"); + } + + public static String getRutaFicheroSalidaCiclos() { + return PROPIEDADES_CONFIGURACION.getProperty("salida.ciclos.ruta.fichero"); + } + + public static String getRutaFicheroSalidaModulos() { + return PROPIEDADES_CONFIGURACION.getProperty("salida.modulos.ruta.fichero"); + } + + public static String getRutaFicheroInformeCiclos() { + return PROPIEDADES_CONFIGURACION.getProperty("salida.informe.ruta.fichero"); } }