From 418921b2fa2f6166b48f90e3268f4e3da97b2254 Mon Sep 17 00:00:00 2001 From: JoanMoncho Date: Sat, 26 Nov 2022 12:20:45 +0100 Subject: [PATCH] commit --- .gitignore | 38 +++++++++++ .idea/encodings.xml | 7 +++ .idea/misc.xml | 14 +++++ .idea/vcs.xml | 6 ++ .idea/workspace.xml | 87 ++++++++++++++++++++++++++ README.md | 38 +++++++++++ pom.xml | 17 +++++ src/main/java/org/joanmoncho/Main.java | 54 ++++++++++++++++ 8 files changed, 261 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 README.md create mode 100644 pom.xml create mode 100644 src/main/java/org/joanmoncho/Main.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..9902dc5 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..e166a41 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,87 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1669459465687 + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..12c224c --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +# Aplicación 10 La lectura de este archivo XML + +1. Primero creamos el xml llamado empleados.xml, donde añadimos el id, el nombre, +el apellido, el departamento y el salario. +2. Luego creamos una instacia de DocumentBuilderFactory, donde cargara el documento con el método parser(); +3. Despues obtenemos la lista de nodos con el nombre empleado de todo el documento +4. Luego con un bucle recorremos la lista +5. Por ultimo obtenemos las etiquetas y valores utilizando la función getNodeType(); + + +## RESULTADO: +``` +Numero de empleados: 4 + +ID del empleado: 1 +Nombre: Joan +Apellido: Moncho +Departamento: 10 +Salario: 999999 + +ID del empleado: 2 +Nombre: Sergi +Apellido: Signes +Departamento: 1 +Salario: 5000 + +ID del empleado: 3 +Nombre: Hector +Apellido: Ferrandiz +Departamento: 1 +Salario: 1 + +ID del empleado: 4 +Nombre: Paco +Apellido: Mengual +Departamento: 25 +Salario: 100 +``` diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..89c9d35 --- /dev/null +++ b/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + org.example + Actividad10 + 1.0-SNAPSHOT + + + 19 + 19 + UTF-8 + + + \ No newline at end of file diff --git a/src/main/java/org/joanmoncho/Main.java b/src/main/java/org/joanmoncho/Main.java new file mode 100644 index 0000000..cbb842a --- /dev/null +++ b/src/main/java/org/joanmoncho/Main.java @@ -0,0 +1,54 @@ +package org.joanmoncho; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.*; + +public class Main { + static File fichero; + + public static void main(String[] args) throws IOException { + + fichero = new File("C:\\Users\\JoanMoncho\\Documents\\ActividadesJuanjo\\empleados.xml"); + + short + parser(); + + } + + public static void parser(){ + + try { + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.parse(fichero); + doc.getDocumentElement().normalize(); + + NodeList nList = doc.getElementsByTagName("empleado"); + + System.out.println("Numero de empleados: " + nList.getLength()); + + for(int temp = 0; temp < nList.getLength(); temp++) { + Node node = nList.item(temp); + + if(node.getNodeType() == Node.ELEMENT_NODE) { + Element element = (Element) node; + + System.out.println("\nID del empleado: " + element.getAttribute("id")); + System.out.println("Nombre: " + element.getElementsByTagName("nombre").item(0).getTextContent()); + System.out.println("Apellido: " + element.getElementsByTagName("apellido").item(0).getTextContent()); + System.out.println("Departamento: " + element.getElementsByTagName("departamento").item(0).getTextContent()); + System.out.println("Salario: " + element.getElementsByTagName("salario").item(0).getTextContent()); + } + } + } catch(Exception e) { + e.printStackTrace(); + } + } + +} \ No newline at end of file