Actividad 1.12 Tratamiento de ficheros XML con DOM II
This commit is contained in:
Binary file not shown.
@@ -1 +1 @@
|
||||
xml.ruta = C:/Users/gonza/aadd/fic11/informacion-IES.xml
|
||||
xml.ruta = C:/Users/gonza/aadd/fic12/informacion-IES_v2.xml
|
||||
|
||||
+231
-73
@@ -3,11 +3,16 @@ package main;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Properties;
|
||||
import java.util.Scanner;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.OutputKeys;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
@@ -16,89 +21,242 @@ import org.w3c.dom.NodeList;
|
||||
|
||||
public class Main {
|
||||
|
||||
private static final String PROPERTIES = "config/application.properties";
|
||||
static String rutaXml = "informacion-IES_v2.xml";
|
||||
|
||||
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();
|
||||
cargarConfiguracion();
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int opcion = -1;
|
||||
|
||||
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");
|
||||
do {
|
||||
System.out.println();
|
||||
System.out.println("=== TRATAMIENTO XML CON DOM ===");
|
||||
System.out.println("1. Crear info_IES.xml");
|
||||
System.out.println("2. Crear resumen_ciclos_IES.xml");
|
||||
System.out.println("3. Crear modulos1_ciclo_DAM.xml");
|
||||
System.out.println("4. Crear informe_ciclos_IES.txt");
|
||||
System.out.println("0. Salir");
|
||||
System.out.print("Opcion: ");
|
||||
|
||||
try {
|
||||
opcion = Integer.parseInt(sc.nextLine().trim());
|
||||
} catch (NumberFormatException e) {
|
||||
opcion = -1;
|
||||
}
|
||||
|
||||
try {
|
||||
if (opcion == 1) {
|
||||
crearInfoIes();
|
||||
} else if (opcion == 2) {
|
||||
crearResumenCiclos();
|
||||
} else if (opcion == 3) {
|
||||
crearModulosDam();
|
||||
} else if (opcion == 4) {
|
||||
crearInformeTxt();
|
||||
} else if (opcion == 0) {
|
||||
System.out.println("Hasta luego.");
|
||||
} else {
|
||||
System.out.println("Opcion no valida.");
|
||||
}
|
||||
} while (opcion != 0);
|
||||
sc.close();
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error: " + e.getMessage());
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error: " + e.getMessage());
|
||||
}
|
||||
} while (opcion != 0);
|
||||
|
||||
sc.close();
|
||||
}
|
||||
|
||||
static void crearInfoIes() throws Exception {
|
||||
Element raizOrigen = leerXml();
|
||||
|
||||
Document doc = nuevoDocumento();
|
||||
Element instituto = doc.createElement("instituto");
|
||||
doc.appendChild(instituto);
|
||||
|
||||
Element nombre = doc.createElement("nombre");
|
||||
nombre.setTextContent(texto(raizOrigen, "nombre"));
|
||||
instituto.appendChild(nombre);
|
||||
|
||||
guardarXml(doc, "info_IES.xml");
|
||||
System.out.println("Fichero info_IES.xml creado.");
|
||||
}
|
||||
|
||||
static void crearResumenCiclos() throws Exception {
|
||||
Element raizOrigen = leerXml();
|
||||
|
||||
Document doc = nuevoDocumento();
|
||||
Element resumen = doc.createElement("resumen");
|
||||
doc.appendChild(resumen);
|
||||
|
||||
Element instituto = doc.createElement("instituto");
|
||||
instituto.setTextContent(texto(raizOrigen, "nombre"));
|
||||
instituto.setAttribute("tipo", raizOrigen.getAttribute("tipo"));
|
||||
resumen.appendChild(instituto);
|
||||
|
||||
Element ciclos = doc.createElement("ciclos");
|
||||
resumen.appendChild(ciclos);
|
||||
|
||||
NodeList listaCiclos = raizOrigen.getElementsByTagName("ciclo");
|
||||
for (int i = 0; i < listaCiclos.getLength(); i++) {
|
||||
Element cicloOrigen = (Element) listaCiclos.item(i);
|
||||
|
||||
Element ciclo = doc.createElement("ciclo");
|
||||
ciclo.setTextContent(texto(cicloOrigen, "nombre_corto"));
|
||||
ciclo.setAttribute("codigo", cicloOrigen.getAttribute("codigo"));
|
||||
ciclos.appendChild(ciclo);
|
||||
}
|
||||
|
||||
guardarXml(doc, "resumen_ciclos_IES.xml");
|
||||
System.out.println("Fichero resumen_ciclos_IES.xml creado.");
|
||||
}
|
||||
|
||||
static void crearModulosDam() throws Exception {
|
||||
Element raizOrigen = leerXml();
|
||||
|
||||
Document doc = nuevoDocumento();
|
||||
Element ies = doc.createElement("ies");
|
||||
ies.setAttribute("nombre", texto(raizOrigen, "nombre"));
|
||||
ies.setAttribute("tipo", raizOrigen.getAttribute("tipo"));
|
||||
doc.appendChild(ies);
|
||||
|
||||
boolean encontrado = false;
|
||||
NodeList listaCiclos = raizOrigen.getElementsByTagName("ciclo");
|
||||
|
||||
for (int i = 0; i < listaCiclos.getLength(); i++) {
|
||||
Element cicloOrigen = (Element) listaCiclos.item(i);
|
||||
|
||||
if (!texto(cicloOrigen, "nombre_corto").equals("DAM")) {
|
||||
continue;
|
||||
}
|
||||
encontrado = true;
|
||||
|
||||
Element ciclo = doc.createElement("ciclo");
|
||||
ciclo.setAttribute("codigo", cicloOrigen.getAttribute("codigo"));
|
||||
ciclo.setAttribute("nivel", cicloOrigen.getAttribute("nivel"));
|
||||
ies.appendChild(ciclo);
|
||||
|
||||
Element nombreCorto = doc.createElement("nombre_corto");
|
||||
nombreCorto.setTextContent(texto(cicloOrigen, "nombre_corto"));
|
||||
ciclo.appendChild(nombreCorto);
|
||||
|
||||
Element nombreLargo = doc.createElement("nombre_largo");
|
||||
nombreLargo.setTextContent(texto(cicloOrigen, "nombre_largo"));
|
||||
ciclo.appendChild(nombreLargo);
|
||||
|
||||
NodeList cursos = cicloOrigen.getElementsByTagName("curso");
|
||||
for (int j = 0; j < cursos.getLength(); j++) {
|
||||
Element cursoOrigen = (Element) cursos.item(j);
|
||||
|
||||
if (!cursoOrigen.getAttribute("numero").equals("1")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Element curso = doc.createElement("curso");
|
||||
curso.setAttribute("num", "1");
|
||||
ciclo.appendChild(curso);
|
||||
|
||||
NodeList modulos = cursoOrigen.getElementsByTagName("modulo");
|
||||
for (int k = 0; k < modulos.getLength(); k++) {
|
||||
Element moduloOrigen = (Element) modulos.item(k);
|
||||
|
||||
Element modulo = doc.createElement("modulo");
|
||||
modulo.setAttribute("id", moduloOrigen.getAttribute("id"));
|
||||
modulo.setAttribute("ref", moduloOrigen.getAttribute("ref"));
|
||||
modulo.setAttribute("numero_horas", texto(moduloOrigen, "numerohoras"));
|
||||
curso.appendChild(modulo);
|
||||
|
||||
Element titulo = doc.createElement("titulo");
|
||||
titulo.setTextContent(texto(moduloOrigen, "titulo"));
|
||||
modulo.appendChild(titulo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!encontrado) {
|
||||
System.out.println("No se ha encontrado el ciclo DAM.");
|
||||
return;
|
||||
}
|
||||
|
||||
guardarXml(doc, "modulos1_ciclo_DAM.xml");
|
||||
System.out.println("Fichero modulos1_ciclo_DAM.xml creado.");
|
||||
}
|
||||
|
||||
static void crearInformeTxt() throws Exception {
|
||||
Element raizOrigen = leerXml();
|
||||
|
||||
PrintWriter pw = new PrintWriter("informe_ciclos_IES.txt", "UTF-8");
|
||||
|
||||
pw.println("INFORME DE CICLOS FORMATIVOS");
|
||||
pw.println(texto(raizOrigen, "nombre"));
|
||||
pw.println("========================================");
|
||||
|
||||
NodeList listaCiclos = raizOrigen.getElementsByTagName("ciclo");
|
||||
for (int i = 0; i < listaCiclos.getLength(); i++) {
|
||||
Element ciclo = (Element) listaCiclos.item(i);
|
||||
|
||||
pw.println();
|
||||
pw.println("Código: " + ciclo.getAttribute("codigo"));
|
||||
pw.println("Nombre corto: " + texto(ciclo, "nombre_corto"));
|
||||
pw.println("Observaciones:");
|
||||
|
||||
NodeList hijos = ciclo.getChildNodes();
|
||||
for (int j = 0; j < hijos.getLength(); j++) {
|
||||
Node hijo = hijos.item(j);
|
||||
if (hijo.getNodeType() == Node.COMMENT_NODE) {
|
||||
pw.println("- " + hijo.getNodeValue().trim());
|
||||
}
|
||||
}
|
||||
|
||||
pw.println();
|
||||
if (i < listaCiclos.getLength() - 1) {
|
||||
pw.println("----------------------------------------");
|
||||
} else {
|
||||
pw.println("========================================");
|
||||
}
|
||||
}
|
||||
|
||||
pw.println("Fin del informe");
|
||||
pw.close();
|
||||
|
||||
System.out.println("Fichero informe_ciclos_IES.txt creado.");
|
||||
}
|
||||
|
||||
static void cargarConfiguracion() {
|
||||
Properties p = new Properties();
|
||||
try {
|
||||
FileInputStream in = new FileInputStream("config/application.properties");
|
||||
p.load(in);
|
||||
in.close();
|
||||
rutaXml = p.getProperty("xml.ruta", rutaXml);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Sin application.properties, se usa " + rutaXml);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
static Element leerXml() throws Exception {
|
||||
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(rutaXml));
|
||||
return doc.getDocumentElement();
|
||||
}
|
||||
|
||||
static Document nuevoDocumento() throws Exception {
|
||||
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
|
||||
doc.setXmlStandalone(true);
|
||||
return doc;
|
||||
}
|
||||
|
||||
private static void imprimirInstituto(Document doc) {
|
||||
Element ins = doc.getDocumentElement();
|
||||
System.out.println("Instituto: " + ins.getAttribute("nombre")
|
||||
+ " | Tipo: " + ins.getAttribute("tipo"));
|
||||
static void guardarXml(Document doc, String ruta) throws Exception {
|
||||
Transformer t = TransformerFactory.newInstance().newTransformer();
|
||||
t.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
|
||||
t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
|
||||
t.transform(new DOMSource(doc), new StreamResult(new File(ruta)));
|
||||
}
|
||||
|
||||
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"));
|
||||
static String texto(Element padre, String nombre) {
|
||||
NodeList lista = padre.getElementsByTagName(nombre);
|
||||
if (lista.getLength() == 0) {
|
||||
return "";
|
||||
}
|
||||
return lista.item(0).getTextContent().trim();
|
||||
}
|
||||
|
||||
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