| @ -0,0 +1,6 @@ | |||||
| <?xml version="1.0" encoding="UTF-8"?> | |||||
| <classpath> | |||||
| <classpathentry kind="src" path="src"/> | |||||
| <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | |||||
| <classpathentry kind="output" path="bin"/> | |||||
| </classpath> | |||||
| @ -0,0 +1,17 @@ | |||||
| <?xml version="1.0" encoding="UTF-8"?> | |||||
| <projectDescription> | |||||
| <name>Ejercicio_10</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> | |||||
| @ -0,0 +1,18 @@ | |||||
| <?xml version="1.0" encoding="UTF-8"?> | |||||
| <Empleados> | |||||
| <empleado id="1"> | |||||
| <apellido>Martinez</apellido> | |||||
| <dep>10</dep> | |||||
| <salario>1000.33</salario> | |||||
| </empleado> | |||||
| <empleado id="2"> | |||||
| <apellido>Garcia</apellido> | |||||
| <dep>11</dep> | |||||
| <salario>2000.34</salario> | |||||
| </empleado> | |||||
| <empleado id="3"> | |||||
| <apellido>Navarro</apellido> | |||||
| <dep>10</dep> | |||||
| <salario>2222.33</salario> | |||||
| </empleado> | |||||
| </Empleados> | |||||
| @ -1,2 +1,35 @@ | |||||
| # Ejercicio_10 | # Ejercicio_10 | ||||
| ### Introduccion | |||||
| Ejercicio de lectura de un arachivo ".xml" con DOM | |||||
| ### Documentacion | |||||
| ~~~ | |||||
| import java.io.File; | |||||
| import javax.xml.parsers.*; | |||||
| import org.w3c.dom.*; | |||||
| ~~~ | |||||
| ### Salida por pantalla | |||||
| ~~~ | |||||
| Empleados : 3 | |||||
| ----------------------- | |||||
| id: 1 | |||||
| apellido: Martinez | |||||
| departamento: 10 | |||||
| salario: 1000.33 | |||||
| ----------------------- | |||||
| id: 2 | |||||
| apellido: Garcia | |||||
| departamento: 11 | |||||
| salario: 2000.34 | |||||
| ----------------------- | |||||
| id: 3 | |||||
| apellido: Navarro | |||||
| departamento: 10 | |||||
| salario: 2222.33 | |||||
| ~~~ | |||||
| @ -0,0 +1,40 @@ | |||||
| package com.jorpelu; | |||||
| import java.io.File; | |||||
| import javax.xml.parsers.*; | |||||
| import org.w3c.dom.*; | |||||
| public class LecturaXML { | |||||
| public static void main(String[] args) { | |||||
| File file = new File("DOMEmpleados.xml"); | |||||
| try { | |||||
| DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |||||
| DocumentBuilder db = dbf.newDocumentBuilder(); | |||||
| Document doc = db.parse(file); | |||||
| doc.getDocumentElement().normalize(); | |||||
| NodeList lista = doc.getElementsByTagName("empleado"); | |||||
| System.out.println("Empleados : " + lista.getLength()); | |||||
| for (int i = 0; i<lista.getLength(); i++) { | |||||
| Node nodo = lista.item(i); | |||||
| if(nodo.getNodeType() == Node.ELEMENT_NODE) { | |||||
| Element elemento = (Element) nodo; | |||||
| System.out.println("----------------------- \n id: " + elemento.getAttribute("id")); | |||||
| System.out.println("apellido: " + elemento.getElementsByTagName("apellido").item(0).getTextContent()); | |||||
| System.out.println("departamento: " + elemento.getElementsByTagName("dep").item(0).getTextContent()); | |||||
| System.out.println("salario: " + elemento.getElementsByTagName("salario").item(0).getTextContent()); | |||||
| } | |||||
| } | |||||
| } catch (Exception e) { | |||||
| System.out.println("No se ha leido el archivo correctamente \n error aqui: " + e.getLocalizedMessage()); | |||||
| } | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,10 @@ | |||||
| /** | |||||
| * | |||||
| */ | |||||
| /** | |||||
| * @author jorge | |||||
| * | |||||
| */ | |||||
| module Ejercicio_10 { | |||||
| requires java.xml; | |||||
| } | |||||