Mostrar arquivo xml numa JTextArea

3 respostas
D
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.FileReader;
import java.io.IOException;
import java.io.File;
import java.io.IOException;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class Main extends JPanel  {

    public Main() {
        super(new GridLayout());
 final JTextArea jt= new JTextArea("dear test :) ",5,20);
 
   

    SAXBuilder builder = new SAXBuilder();
    	File xmlFile = new File("file.xml");
       jt.File( );
        try{

    	   Document document = (Document) builder.build(xmlFile);
           Element rootNode = document.getRootElement();
           List list = rootNode.getChildren("staff");

           for (int i=0; i< list.size(); i++)
           {
             Element node = (Element) list.get(i);

             System.out.println("First Name : "  + node.getChildText("firstname"));
       	     System.out.println("Last Name : "  + node.getChildText("lastname"));
      	     System.out.println("Nick Name : "  + node.getChildText("nickname"));
      	     System.out.println("Salary : "  + node.getChildText("salary"));
             jt.read(node.getChildText("salary"));
             
           }

    	 }catch(IOException io){
    		System.out.println(io.getMessage());
    	 }catch(JDOMException jdomex){
    		System.out.println(jdomex.getMessage());
    	}

 add(jt);

    
    }

   
    

    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("TextDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add contents to the window.
        frame.add(new Main());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
       
          frame.setLocationRelativeTo(null);

    }

    public static void main(String[] args) {
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

 }

Pretendo visualizar o conteudo do arquivo file.xml na JTextArea jt , como faco isso?

Valeu []

3 Respostas

ViniGodoy

Carregue o xml diretamente, como se fosse um arquivo texto. Não como uma arvore DOM.

Caso contrário, você terá que usar um transformer, mas é uma perda de tempo, já que vc já tem o arquivo mesmo.

D

queria mostrar apenas o conteudo da file, nao a file toda

node.getChildText(firstname)

node.getChildText(lastname)

node.getChildText(nickname)

node.getChildText(salary)
D

o output do programa e

First Name : yong
Last Name : mook kim
Nick Name : mkyong
Salary : 100000
First Name : low
Last Name : yin fong
Nick Name : fong fong
Salary : 200000

mas , eu pretendo que este output , seja mostrado dentro do jtextarea, como faco isso?

Criado 13 de abril de 2010
Ultima resposta 14 de abr. de 2010
Respostas 3
Participantes 2