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