elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Como proteger una cartera - billetera de Bitcoin


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  Ayuda con Java-XML-DOM
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Ayuda con Java-XML-DOM  (Leído 1,628 veces)
istok55

Desconectado Desconectado

Mensajes: 2


Ver Perfil
Ayuda con Java-XML-DOM
« en: 10 Diciembre 2013, 22:00 pm »

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


Código
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <EMPLEADOS>
  3.   <EMPLEADO>
  4.     <NOMBRE>LUIS MIGUEL</NOMBRE>
  5.     <APELLIDOS>GALLARDO LEON</APELLIDOS>
  6.     <NIF>5674179P</NIF>
  7.     <EMPRESA>DATATONIC, S.A.</EMPRESA>
  8.     <DIRECCION>HERMANOS ARAGON , CALLE</DIRECCION>
  9.     <CP>11690</CP>
  10.     <LOCALIDAD>OLVERA</LOCALIDAD>
  11.     <TLF_FIJO>95630316</TLF_FIJO>
  12.     <TLF_MOVIL>69619915</TLF_MOVIL>
  13.   </EMPLEADO>
  14.   <EMPLEADO>
  15.     <NOMBRE>MIGUEL</NOMBRE>
  16.     <APELLIDOS>BERENGUEL MARQUINA</APELLIDOS>
  17.     <NIF>6182669F</NIF>
  18.     <EMPRESA>MAGARIIT SERVICES, S.A.</EMPRESA>
  19.     <DIRECCION>ALCALA LA REAL , CALLE</DIRECCION>
  20.     <CP>11640</CP>
  21.     <LOCALIDAD>BORNOS</LOCALIDAD>
  22.     <TLF_FIJO>96304400</TLF_FIJO>
  23.     <TLF_MOVIL>62676864</TLF_MOVIL>
  24.   </EMPLEADO>
  25. </EMPLEADOS>

Código java


Código
  1. import javax.xml.parsers.DocumentBuilderFactory;
  2. import javax.xml.parsers.DocumentBuilder;
  3. import org.w3c.dom.Document;
  4. import org.w3c.dom.NodeList;
  5. import org.w3c.dom.Node;
  6. import org.w3c.dom.Element;
  7. import java.io.File;
  8.  
  9.  
  10.  
  11. public class ver {
  12.  
  13. public static void main(String argv[]) {
  14.  
  15.    try {
  16.  
  17. File fXmlFile = new File("staff.xml");
  18. DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  19. DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
  20. Document doc = dBuilder.parse(fXmlFile);
  21.  
  22. //optional, but recommended
  23. //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
  24. doc.getDocumentElement().normalize();
  25.  
  26. System.out.println("Elemento Raiz :" + doc.getDocumentElement().getNodeName());
  27.  
  28. NodeList nList = doc.getElementsByTagName("empleado");
  29.  
  30. System.out.println("----------------------------");
  31.  
  32. for (int temp = 0; temp < nList.getLength(); temp++) {
  33. Node nNode = nList.item(temp);
  34.  
  35. System.out.println("\nElemento :" + nNode.getNodeName());
  36.  
  37. if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  38.  
  39. Element eElement = (Element) nNode;
  40.  
  41. System.out.println("id empleado : " + eElement.getAttribute("id"));
  42. System.out.println("Nombre : " + eElement.getElementsByTagName("nombre").item(0).getTextContent());
  43. System.out.println("Apellidos : " + eElement.getElementsByTagName("apellidos").item(0).getTextContent());
  44. System.out.println("Puesto : " + eElement.getElementsByTagName("puesto").item(0).getTextContent());
  45. System.out.println("Salario : " + eElement.getElementsByTagName("salario").item(0).getTextContent());
  46.  
  47. }
  48. }
  49.    } catch (Exception e) {
  50. e.printStackTrace();
  51.    }
  52.  }
  53.  
  54. }
         
   
      





« Última modificación: 11 Diciembre 2013, 00:35 am por Leyer » En línea

istok55

Desconectado Desconectado

Mensajes: 2


Ver Perfil
Re: Ayuda con Java-XML-DOM
« Respuesta #1 en: 11 Diciembre 2013, 03:45 am »

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  :-\


En línea

Leyer


Desconectado Desconectado

Mensajes: 786


leyer@elhacker.net


Ver Perfil WWW
Re: Ayuda con Java-XML-DOM
« Respuesta #2 en: 11 Diciembre 2013, 05:48 am »

Pero postea el codigo que llevas
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
AYUDA JAVA « 1 2 »
Java
jauma123 10 8,660 Último mensaje 1 Marzo 2004, 16:43 pm
por jauma123
ayuda en java
Java
drakman 0 3,112 Último mensaje 1 Junio 2004, 11:16 am
por drakman
ayuda con java
Java
coolfrog 1 2,545 Último mensaje 12 Diciembre 2004, 08:00 am
por cimi
ayuda con java
Java
tormenta 2 2,580 Último mensaje 27 Abril 2005, 16:39 pm
por tormenta
Ayuda con Java!
Java
McKain 1 2,352 Último mensaje 30 Julio 2005, 01:28 am
por heap
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines