|
|
@ -0,0 +1,40 @@ |
|
|
|
package lecturaXML; |
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
|
|
|
|
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 Main { |
|
|
|
public static void main(String[] args) { |
|
|
|
try { |
|
|
|
File fil = new File("src/lecturaXML/Empleados.xml"); |
|
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
|
|
|
DocumentBuilder builder = factory.newDocumentBuilder(); |
|
|
|
Document document = builder.parse(new File("src/lecturaXML/Empleados.xml")); |
|
|
|
NodeList empleados = document.getElementsByTagName("empleado"); |
|
|
|
for (int i = 0; i < empleados.getLength(); i++) { |
|
|
|
Node node = empleados.item(i); |
|
|
|
if (node.getNodeType() == Node.ELEMENT_NODE) { |
|
|
|
Element eElement = (Element) node; |
|
|
|
if(eElement.hasChildNodes()) { |
|
|
|
NodeList nl = node.getChildNodes(); |
|
|
|
for(int j=0; j<nl.getLength(); j++) { |
|
|
|
Node nd = nl.item(j); |
|
|
|
System.out.println(nd.getTextContent()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
System.out.println("Número de empleados: " + empleados.getLength()); |
|
|
|
} catch(Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |