Primer commit
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
# Compilados de Java
|
||||||
|
*.class
|
||||||
|
bin/
|
||||||
|
|
||||||
|
# Configuración de Eclipse
|
||||||
|
.project
|
||||||
|
.classpath
|
||||||
|
.settings/
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
instituto.ruta.fichero=D:/Documentos/DAM 2/AC - Acceso a Datos/Practicas/aadd/fic11/informacion-IES.xml
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package org.palomafp.aadd.smmb;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.File;
|
||||||
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
|
public class LectorInstitutoXml {
|
||||||
|
|
||||||
|
private final String rutaXmlInstituto;
|
||||||
|
|
||||||
|
public LectorInstitutoXml(String ruta) {
|
||||||
|
rutaXmlInstituto = ruta;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Document cargarDocumento() throws ParserConfigurationException, SAXException, IOException {
|
||||||
|
|
||||||
|
DocumentBuilderFactory fabrica = DocumentBuilderFactory.newInstance();
|
||||||
|
DocumentBuilder constructor = fabrica.newDocumentBuilder();
|
||||||
|
Document documento = constructor.parse(new File(rutaXmlInstituto));
|
||||||
|
|
||||||
|
|
||||||
|
return documento;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,168 @@
|
|||||||
|
package org.palomafp.aadd.smmb;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
|
import org.palomafp.aadd.smmb.gestores.GestorConfiguracion;
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.w3c.dom.Element;
|
||||||
|
import org.w3c.dom.NodeList;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
|
public class MainInstitutos {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
|
||||||
|
Document documento = null;
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
MainInstitutos gm = new MainInstitutos();
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
GestorConfiguracion configuracion = new GestorConfiguracion("application.properties");
|
||||||
|
String ruta = configuracion.getRutaInstituto();
|
||||||
|
|
||||||
|
LectorInstitutoXml lector = new LectorInstitutoXml(ruta);
|
||||||
|
documento = lector.cargarDocumento();
|
||||||
|
|
||||||
|
}catch (ParserConfigurationException | SAXException | IOException e) {
|
||||||
|
System.err.println("No se ha podido leer el fichero XML: " + e.getMessage());
|
||||||
|
//Esto lo ponemos para que en el caso de que el programa falle al cargar no sigua y de error por no tener el documento cargado
|
||||||
|
sc.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("1. Imprimir instituto");
|
||||||
|
System.out.println("2. Imprimir ciclos");
|
||||||
|
System.out.println("3. Imprimir módulos filtro 1");
|
||||||
|
System.out.println("4. Imprimir módulos filtro 2");
|
||||||
|
System.out.print("Elige una opción: ");
|
||||||
|
|
||||||
|
String opcion = sc.nextLine();
|
||||||
|
|
||||||
|
|
||||||
|
switch(opcion) {
|
||||||
|
|
||||||
|
case "1":
|
||||||
|
gm.imprimirInstituto(documento);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "2":
|
||||||
|
gm.imprimirCiclos(documento);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "3":
|
||||||
|
gm.imprimirModulos1(documento);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "4":
|
||||||
|
gm.imprimirModulos2(documento);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
System.out.println("Opción no válida");
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
sc.close();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void imprimirInstituto(Document documento) {
|
||||||
|
|
||||||
|
Element raiz = documento.getDocumentElement();
|
||||||
|
String nombre = raiz.getAttribute("nombre");
|
||||||
|
String tipo = raiz.getAttribute("tipo");
|
||||||
|
System.out.println("Instituto: " + nombre + " (" + tipo + ")");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void imprimirCiclos(Document documento) {
|
||||||
|
|
||||||
|
// Pedimos todos los elementos que se llamen ciclo
|
||||||
|
NodeList lCiclos = documento.getElementsByTagName("ciclo");
|
||||||
|
|
||||||
|
// Damos una vuelta por cada ciclo que nos ha traido
|
||||||
|
for (int i = 0; i < lCiclos.getLength(); i++) {
|
||||||
|
|
||||||
|
// Devuelve un Node, que es generico, hay que decirle que es un elemento con etiqueta
|
||||||
|
Element ciclo = (Element) lCiclos.item(i);
|
||||||
|
|
||||||
|
String codigo = ciclo.getAttribute("codigo");
|
||||||
|
|
||||||
|
// Dentro del ciclo buscamos los elementos que se llamen "nombre_corto"
|
||||||
|
NodeList lNombres = ciclo.getElementsByTagName("nombre_corto");
|
||||||
|
// Leemos el texto que tiene dentro. No hace falta castear porque getTextContent acepta que se le pase un Nodo
|
||||||
|
String nombre = lNombres.item(0).getTextContent();
|
||||||
|
|
||||||
|
System.out.println(codigo + " - " + nombre);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void imprimirModulos1(Document documento) {
|
||||||
|
|
||||||
|
// Pedimos todos los elementos que se llamen modulo
|
||||||
|
NodeList lModulos = documento.getElementsByTagName("modulo");
|
||||||
|
|
||||||
|
for (int i = 0; i < lModulos.getLength(); i++) {
|
||||||
|
|
||||||
|
Element modulo = (Element) lModulos.item(i);
|
||||||
|
|
||||||
|
// Buscamos el titulo, sabemos que solo hay 1 asi que buscamos en el 0, y leemos el texto de dentro
|
||||||
|
String titulo = modulo.getElementsByTagName("titulo").item(0).getTextContent().trim();
|
||||||
|
|
||||||
|
if (titulo.startsWith("Programa")) {
|
||||||
|
String id = modulo.getAttribute("id");
|
||||||
|
String ref = modulo.getAttribute("ref");
|
||||||
|
String horas = modulo.getElementsByTagName("numerohoras").item(0).getTextContent();
|
||||||
|
|
||||||
|
System.out.println("id=" + id + ", ref=" + ref + ", titulo=" + titulo + ", horas=" + horas);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void imprimirModulos2(Document documento) {
|
||||||
|
|
||||||
|
NodeList lCurso = documento.getElementsByTagName("curso");
|
||||||
|
|
||||||
|
for (int i = 0; i < lCurso.getLength(); i++){
|
||||||
|
|
||||||
|
Element curso = (Element) lCurso.item(i);
|
||||||
|
|
||||||
|
String numero = curso.getAttribute("numero");
|
||||||
|
|
||||||
|
if (numero.equals("1")) {
|
||||||
|
|
||||||
|
NodeList lModulos = curso.getElementsByTagName("modulo");
|
||||||
|
|
||||||
|
for (int j = 0; j < lModulos.getLength(); j++) {
|
||||||
|
|
||||||
|
Element modulo = (Element) lModulos.item(j);
|
||||||
|
|
||||||
|
String titulo = modulo.getElementsByTagName("titulo").item(0).getTextContent().trim();
|
||||||
|
|
||||||
|
if (titulo.startsWith("Programa")) {
|
||||||
|
String id = modulo.getAttribute("id");
|
||||||
|
String ref = modulo.getAttribute("ref");
|
||||||
|
String horas = modulo.getElementsByTagName("numerohoras").item(0).getTextContent();
|
||||||
|
|
||||||
|
System.out.println("id=" + id + ", ref=" + ref + ", titulo=" + titulo + ", horas=" + horas);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package org.palomafp.aadd.smmb.gestores;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class GestorConfiguracion {
|
||||||
|
|
||||||
|
private final Properties propiedades = new Properties();
|
||||||
|
|
||||||
|
public GestorConfiguracion(String rutaFicheroConfi) {
|
||||||
|
|
||||||
|
try (InputStream is = GestorConfiguracion.class.getClassLoader().getResourceAsStream(rutaFicheroConfi)){
|
||||||
|
|
||||||
|
if (is == null) {
|
||||||
|
//Aquí iría si le pusieramos una excepcion creada por nosotros y añadimos throws al public GestorConfiguracion
|
||||||
|
throw new IOException("El fichero no se ha encontrado");
|
||||||
|
}
|
||||||
|
|
||||||
|
propiedades.load(is);
|
||||||
|
|
||||||
|
}catch (IOException e) {
|
||||||
|
System.err.println("No se ha podido cargar el fichero de configuración: " + e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String getRutaInstituto() {
|
||||||
|
return propiedades.getProperty("instituto.ruta.fichero");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user