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