commit 5704d4d1dcc8f3eb51c2d46a1923ec49ce80805c Author: Daniel Date: Sun Nov 27 20:01:36 2022 +0100 first commit diff --git a/Actividad10/.classpath b/Actividad10/.classpath new file mode 100644 index 0000000..57bca72 --- /dev/null +++ b/Actividad10/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/Actividad10/.project b/Actividad10/.project new file mode 100644 index 0000000..e761406 --- /dev/null +++ b/Actividad10/.project @@ -0,0 +1,17 @@ + + + Actividad10 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/Actividad10/.settings/org.eclipse.core.resources.prefs b/Actividad10/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/Actividad10/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/Actividad10/.settings/org.eclipse.jdt.core.prefs b/Actividad10/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..8c9943d --- /dev/null +++ b/Actividad10/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=17 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=17 diff --git a/Actividad10/README.md b/Actividad10/README.md new file mode 100644 index 0000000..6488974 --- /dev/null +++ b/Actividad10/README.md @@ -0,0 +1,27 @@ +## Título +Actividad 10 - Leer archivo .xml por pantalla + +## Uso +Creamos una clase que sea capaz de interpretar los datos de un archivo xml y mostrarlos por pantalla +``` +idEmpleado: 1 +Apellido: Lautaro +Departamento: 12 +Salario: 1500.99 + +idEmpleado: 2 +Apellido: Rodriguez +Departamento: 1 +Salario: 2900 + +idEmpleado: 3 +Apellido: Martinez +Departamento: 33 +Salario: 2400.50 + +idEmpleado: 4 +Apellido: Conejero +Departamento: 22 +Salario: 6045 + +``` \ No newline at end of file diff --git a/Actividad10/bin/EmpleadoParser.class b/Actividad10/bin/EmpleadoParser.class new file mode 100644 index 0000000..b45b339 Binary files /dev/null and b/Actividad10/bin/EmpleadoParser.class differ diff --git a/Actividad10/bin/Main.class b/Actividad10/bin/Main.class new file mode 100644 index 0000000..21dab57 Binary files /dev/null and b/Actividad10/bin/Main.class differ diff --git a/Actividad10/empleados.xml b/Actividad10/empleados.xml new file mode 100644 index 0000000..01cd037 --- /dev/null +++ b/Actividad10/empleados.xml @@ -0,0 +1,22 @@ + + + Lautaro + 12 + 1500.99 + + + Rodriguez + 1 + 2900 + + + Martinez + 33 + 2400.50 + + + Conejero + 22 + 6045 + + \ No newline at end of file diff --git a/Actividad10/src/EmpleadoParser.java b/Actividad10/src/EmpleadoParser.java new file mode 100644 index 0000000..fea7134 --- /dev/null +++ b/Actividad10/src/EmpleadoParser.java @@ -0,0 +1,46 @@ +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 EmpleadoParser { + + private final InputStream empleadosFile; + + public EmpleadoParser(File location) throws IOException { + empleadosFile = new FileInputStream(location); + } + + public boolean parse() { + boolean parsed = false; + + try { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document dom = builder.parse(empleadosFile); + Element root = dom.getDocumentElement(); + NodeList items = root.getElementsByTagName("empleado"); + + for(int i = 0; i < items.getLength(); i++) { + Node node = items.item(i); + if(node.getNodeType() != Node.ELEMENT_NODE) { + throw new RuntimeException("Error in XML document, check xml syntax"); + } + Element element = (Element) node; + System.out.printf("idEmpleado: %s \n",element.getAttribute("id")); + System.out.printf("Apellido: %s\n",element.getElementsByTagName("apellido").item(0).getTextContent()); + System.out.printf("Departamento: %s\n",element.getElementsByTagName("dep").item(0).getTextContent()); + System.out.printf("Salario: %s\n\n",element.getElementsByTagName("salario").item(0).getTextContent()); + } + parsed = true; + + } catch (Exception e) { + e.printStackTrace(); + } + return parsed; + } +} diff --git a/Actividad10/src/Main.java b/Actividad10/src/Main.java new file mode 100644 index 0000000..77a764c --- /dev/null +++ b/Actividad10/src/Main.java @@ -0,0 +1,14 @@ +import java.io.File; +import java.io.IOException; + +public class Main { + public static void main(String[] args) { + try { + EmpleadoParser empleadoParser = new EmpleadoParser(new File("empleados.xml")); + empleadoParser.parse(); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } +} \ No newline at end of file