Actividad 1.11 Tratamiento de ficheros XML con DOM I
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
bin/
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
|
.settings/
|
||||||
Binary file not shown.
@@ -0,0 +1 @@
|
|||||||
|
xml.ruta = C:/Users/gonza/aadd/fic11/informacion-IES.xml
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
package main;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.w3c.dom.Element;
|
||||||
|
import org.w3c.dom.Node;
|
||||||
|
import org.w3c.dom.NodeList;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
private static final String PROPERTIES = "config/application.properties";
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
Document doc = cargarDocumento();
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
int opcion;
|
||||||
|
do {
|
||||||
|
System.out.println("\n1. 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.println("0. Salir");
|
||||||
|
System.out.print("Opción: ");
|
||||||
|
opcion = sc.hasNextInt() ? sc.nextInt() : -1;
|
||||||
|
sc.nextLine();
|
||||||
|
|
||||||
|
switch (opcion) {
|
||||||
|
case 1 -> imprimirInstituto(doc);
|
||||||
|
case 2 -> imprimirCiclos(doc);
|
||||||
|
case 3 -> imprimirModulos(doc, false);
|
||||||
|
case 4 -> imprimirModulos(doc, true);
|
||||||
|
case 0 -> System.out.println("Adiós");
|
||||||
|
default -> System.out.println("Opción no válida");
|
||||||
|
}
|
||||||
|
} while (opcion != 0);
|
||||||
|
sc.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Error: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Document cargarDocumento() throws Exception {
|
||||||
|
Properties props = new Properties();
|
||||||
|
try (FileInputStream fis = new FileInputStream(PROPERTIES)) {
|
||||||
|
props.load(fis);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IOException("No se pudo leer " + PROPERTIES, e);
|
||||||
|
}
|
||||||
|
File xml = new File(props.getProperty("xml.ruta"));
|
||||||
|
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||||
|
Document doc = builder.parse(xml);
|
||||||
|
doc.getDocumentElement().normalize();
|
||||||
|
return doc;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void imprimirInstituto(Document doc) {
|
||||||
|
Element ins = doc.getDocumentElement();
|
||||||
|
System.out.println("Instituto: " + ins.getAttribute("nombre")
|
||||||
|
+ " | Tipo: " + ins.getAttribute("tipo"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void imprimirCiclos(Document doc) {
|
||||||
|
NodeList ciclos = doc.getElementsByTagName("ciclo");
|
||||||
|
for (int i = 0; i < ciclos.getLength(); i++) {
|
||||||
|
Element ciclo = (Element) ciclos.item(i);
|
||||||
|
System.out.println("Código: " + ciclo.getAttribute("codigo")
|
||||||
|
+ " | Nombre corto: " + textoHijo(ciclo, "nombre_corto"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void imprimirModulos(Document doc, boolean soloCurso1) {
|
||||||
|
NodeList modulos = doc.getElementsByTagName("modulo");
|
||||||
|
for (int i = 0; i < modulos.getLength(); i++) {
|
||||||
|
Element mod = (Element) modulos.item(i);
|
||||||
|
|
||||||
|
String titulo = textoHijo(mod, "titulo");
|
||||||
|
if (!titulo.startsWith("Programa")) continue;
|
||||||
|
|
||||||
|
Element curso = (Element) mod.getParentNode();
|
||||||
|
String numCurso = curso.getAttribute("numero");
|
||||||
|
if (soloCurso1 && !"1".equals(numCurso)) continue;
|
||||||
|
|
||||||
|
System.out.println("Id: " + mod.getAttribute("id")
|
||||||
|
+ " | Ref: " + mod.getAttribute("ref")
|
||||||
|
+ " | Título: " + titulo
|
||||||
|
+ " | Horas: " + textoHijo(mod, "numerohoras")
|
||||||
|
+ (soloCurso1 ? "" : " | Curso: " + numCurso));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String textoHijo(Element padre, String etiqueta) {
|
||||||
|
NodeList l = padre.getElementsByTagName(etiqueta);
|
||||||
|
return l.getLength() > 0 ? l.item(0).getTextContent().trim() : "";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user