Browse Source

first commit

master
Daniel 1 year ago
commit
5704d4d1dc
10 changed files with 152 additions and 0 deletions
  1. +10
    -0
      Actividad10/.classpath
  2. +17
    -0
      Actividad10/.project
  3. +2
    -0
      Actividad10/.settings/org.eclipse.core.resources.prefs
  4. +14
    -0
      Actividad10/.settings/org.eclipse.jdt.core.prefs
  5. +27
    -0
      Actividad10/README.md
  6. BIN
      Actividad10/bin/EmpleadoParser.class
  7. BIN
      Actividad10/bin/Main.class
  8. +22
    -0
      Actividad10/empleados.xml
  9. +46
    -0
      Actividad10/src/EmpleadoParser.java
  10. +14
    -0
      Actividad10/src/Main.java

+ 10
- 0
Actividad10/.classpath View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

+ 17
- 0
Actividad10/.project View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Actividad10</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

+ 2
- 0
Actividad10/.settings/org.eclipse.core.resources.prefs View File

@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8

+ 14
- 0
Actividad10/.settings/org.eclipse.jdt.core.prefs View File

@ -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

+ 27
- 0
Actividad10/README.md View File

@ -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
```

BIN
Actividad10/bin/EmpleadoParser.class View File


BIN
Actividad10/bin/Main.class View File


+ 22
- 0
Actividad10/empleados.xml View File

@ -0,0 +1,22 @@
<empleados>
<empleado id="1">
<apellido>Lautaro</apellido>
<dep>12</dep>
<salario>1500.99</salario>
</empleado>
<empleado id="2">
<apellido>Rodriguez</apellido>
<dep>1</dep>
<salario>2900</salario>
</empleado>
<empleado id="3">
<apellido>Martinez</apellido>
<dep>33</dep>
<salario>2400.50</salario>
</empleado>
<empleado id="4">
<apellido>Conejero</apellido>
<dep>22</dep>
<salario>6045</salario>
</empleado>
</empleados>

+ 46
- 0
Actividad10/src/EmpleadoParser.java View File

@ -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;
}
}

+ 14
- 0
Actividad10/src/Main.java View File

@ -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);
}
}
}

Loading…
Cancel
Save