|
|
@ -0,0 +1,55 @@ |
|
|
|
package com.cristobalbernal.Actividad10; |
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.FileInputStream; |
|
|
|
import java.io.FileNotFoundException; |
|
|
|
import java.io.InputStream; |
|
|
|
|
|
|
|
import javax.xml.parsers.DocumentBuilder; |
|
|
|
import javax.xml.parsers.DocumentBuilderFactory; |
|
|
|
|
|
|
|
import org.w3c.dom.Document; |
|
|
|
import org.w3c.dom.Element; |
|
|
|
import org.w3c.dom.Node; |
|
|
|
import org.w3c.dom.NodeList; |
|
|
|
|
|
|
|
public class ParserEmpleado { |
|
|
|
private InputStream archivo; |
|
|
|
|
|
|
|
public ParserEmpleado(File localizacion) throws FileNotFoundException { |
|
|
|
archivo = new FileInputStream(localizacion); |
|
|
|
} |
|
|
|
public boolean parse() { |
|
|
|
boolean parsed = false; |
|
|
|
|
|
|
|
try { |
|
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
|
|
|
DocumentBuilder builder = factory.newDocumentBuilder(); |
|
|
|
Document dom = builder.parse(archivo); |
|
|
|
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.println("IdEmpleado: " + element.getAttribute("id")); |
|
|
|
System.out.println("NombreEmpleado: " + element.getElementsByTagName("nombre").item(0).getTextContent()); |
|
|
|
System.out.println("PrimerApellido: " + element.getElementsByTagName("primerApellido").item(0).getTextContent()); |
|
|
|
System.out.println("SegundoApellido: " + element.getElementsByTagName("segundoApellido").item(0).getTextContent()); |
|
|
|
System.out.println("DepartamentoEmpleado: " + element.getElementsByTagName("departamento").item(0).getTextContent()); |
|
|
|
System.out.println("SalarioEmplado: " + element.getElementsByTagName("salario").item(0).getTextContent()); |
|
|
|
System.out.println("\n"); |
|
|
|
|
|
|
|
} |
|
|
|
parsed = true; |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
return parsed; |
|
|
|
} |
|
|
|
|
|
|
|
} |