Hola gente, a ver si alguien puede echarme una mano. Tengo un archivo xml de ejemplo y lo que quiero es leer el xml usando DOM y guardar la salida de DOM a un archivo txt. Dejo el código del archivo xml de ejemplo y el código de DOM con el que leo el xml pero no se como volcar la salida a un txt.
XML
<?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
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 {
public static void main(String argv[]) {
try {
File fXmlFile = new File("staff.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
//optional, but recommended
//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
System.out.println("Elemento Raiz :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("empleado");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nElemento :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("id empleado : " + eElement.getAttribute("id"));
System.out.println("Nombre : " + eElement.getElementsByTagName("nombre").item(0).getTextContent());
System.out.println("Apellidos : " + eElement.getElementsByTagName("apellidos").item(0).getTextContent());
System.out.println("Puesto : " + eElement.getElementsByTagName("puesto").item(0).getTextContent());
System.out.println("Salario : " + eElement.getElementsByTagName("salario").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Vale ya conseguí pasar la salida a un txt. Ahora el problema que tengo es que ese txt lo tengo que pasar a un JTextArea y el textarea no me respeta los saltos de línea :-\
Pero postea el codigo que llevas