|
|
@ -0,0 +1,54 @@ |
|
|
|
package xmladom.com.daniminguet.es; |
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.sql.Connection; |
|
|
|
import java.sql.DriverManager; |
|
|
|
import java.sql.ResultSet; |
|
|
|
import java.sql.SQLException; |
|
|
|
import java.sql.Statement; |
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
import org.w3c.dom.DOMImplementation; |
|
|
|
import org.w3c.dom.Document; |
|
|
|
import org.w3c.dom.Element; |
|
|
|
import org.w3c.dom.Node; |
|
|
|
|
|
|
|
import javax.xml.parsers.DocumentBuilder; |
|
|
|
import javax.xml.parsers.DocumentBuilderFactory; |
|
|
|
|
|
|
|
import org.w3c.dom.NodeList; |
|
|
|
|
|
|
|
public class P10DOM { |
|
|
|
public static void main(String[] args) { |
|
|
|
//Fichero XML donde están guardados los empleados |
|
|
|
File fichero = new File("C:\\Users\\Dani\\OneDrive\\Documentos\\AccesoADatos\\empleados.xml"); |
|
|
|
|
|
|
|
try { |
|
|
|
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); |
|
|
|
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); |
|
|
|
Document doc = dBuilder.parse(fichero); |
|
|
|
doc.getDocumentElement().normalize(); |
|
|
|
|
|
|
|
//Recoge los elementos del archivo que sean "empleado" |
|
|
|
NodeList nList = doc.getElementsByTagName("empleado"); |
|
|
|
//Muestra la cantidad de empleados que hay en el archivo |
|
|
|
System.out.println("Número de empleados: " + nList.getLength()); |
|
|
|
|
|
|
|
//Muestra todos los datos de cada empleado del archivo |
|
|
|
for(int temp = 0; temp < nList.getLength(); temp++) { |
|
|
|
Node node = nList.item(temp); |
|
|
|
|
|
|
|
if(node.getNodeType() == Node.ELEMENT_NODE) { |
|
|
|
Element element = (Element) node; |
|
|
|
|
|
|
|
System.out.println("\nID del empleado: " + element.getAttribute("id")); |
|
|
|
System.out.println("Apellido: " + element.getElementsByTagName("apellido").item(0).getTextContent()); |
|
|
|
System.out.println("Departamento: " + element.getElementsByTagName("dep").item(0).getTextContent()); |
|
|
|
System.out.println("Salario: " + element.getElementsByTagName("salario").item(0).getTextContent()); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch(Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |