|
|
@ -0,0 +1,55 @@ |
|
|
|
import Modelo.Empleado; |
|
|
|
import org.w3c.dom.Node; |
|
|
|
import org.w3c.dom.NodeList; |
|
|
|
import org.w3c.dom.Document; |
|
|
|
import org.w3c.dom.Element; |
|
|
|
|
|
|
|
import javax.xml.parsers.DocumentBuilder; |
|
|
|
import javax.xml.parsers.DocumentBuilderFactory; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.*; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
public class Parser { |
|
|
|
private List<Empleado> empleados; |
|
|
|
private final File empleadoFichero; |
|
|
|
|
|
|
|
public Parser(File file) throws IOException { |
|
|
|
empleadoFichero = file; |
|
|
|
} |
|
|
|
|
|
|
|
public boolean startParser(){ |
|
|
|
boolean parseado = false; |
|
|
|
try{ |
|
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
|
|
|
DocumentBuilder builder = factory.newDocumentBuilder(); |
|
|
|
Document documento = builder.parse(empleadoFichero); |
|
|
|
documento.getDocumentElement().normalize(); |
|
|
|
Element root = documento.getDocumentElement(); |
|
|
|
NodeList nList = root.getElementsByTagName("empleado"); |
|
|
|
empleados = new ArrayList<>(); |
|
|
|
for (int i = 0; i < nList.getLength(); i++) { |
|
|
|
Node node = nList.item(i); |
|
|
|
if (node.getNodeType() != Node.ELEMENT_NODE){ |
|
|
|
throw new RuntimeException(); |
|
|
|
} |
|
|
|
Element element = (Element) node; |
|
|
|
int id = Integer.parseInt(element.getAttribute("id")); |
|
|
|
String apellido = element.getElementsByTagName("apellido").item(0).getTextContent(); |
|
|
|
String departamento = element.getElementsByTagName("dep").item(0).getTextContent(); |
|
|
|
float salario = Float.parseFloat(element.getElementsByTagName("salario").item(0).getTextContent()); |
|
|
|
empleados.add( new Empleado (id,apellido,departamento,salario)); |
|
|
|
} |
|
|
|
parseado = true; |
|
|
|
}catch (Exception e){ |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
return parseado; |
|
|
|
} |
|
|
|
|
|
|
|
public List<Empleado> getEmpleado() { |
|
|
|
return empleados; |
|
|
|
} |
|
|
|
} |