XML
Código
<?xml version="1.0" encoding="UTF-8"?> <EMPLEADOS> <EMPLEADO> <NOMBRE>LUIS MIGUEL</NOMBRE> <APELLIDOS>GALLARDO LEON</APELLIDOS> <NIF>5674179P</NIF> <EMPRESA>DATATONIC, S.A.</EMPRESA> <DIRECCION>HERMANOS ARAGON , CALLE</DIRECCION> <CP>11690</CP> <LOCALIDAD>OLVERA</LOCALIDAD> <TLF_FIJO>95630316</TLF_FIJO> <TLF_MOVIL>69619915</TLF_MOVIL> </EMPLEADO> <EMPLEADO> <NOMBRE>MIGUEL</NOMBRE> <APELLIDOS>BERENGUEL MARQUINA</APELLIDOS> <NIF>6182669F</NIF> <EMPRESA>MAGARIIT SERVICES, S.A.</EMPRESA> <DIRECCION>ALCALA LA REAL , CALLE</DIRECCION> <CP>11640</CP> <LOCALIDAD>BORNOS</LOCALIDAD> <TLF_FIJO>96304400</TLF_FIJO> <TLF_MOVIL>62676864</TLF_MOVIL> </EMPLEADO> </EMPLEADOS>
Código java
Código
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Element; import java.io.File; public class ver { try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); //optional, but recommended //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work doc.getDocumentElement().normalize(); NodeList nList = doc.getElementsByTagName("empleado"); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { System.out.println("Apellidos : " + eElement.getElementsByTagName("apellidos").item(0).getTextContent()); System.out.println("Salario : " + eElement.getElementsByTagName("salario").item(0).getTextContent()); } } e.printStackTrace(); } } }