BondyeLwa
BondyeLwa

Reputation: 27

Attempting to parse an XML and get info onto a GUI

I am trying to get my parsed XML data onto my GUI that I am making. The goal is to click the "Open connection" button (which has the code in it from the class Open Connection) to open the connecting to the XML webpage, then use the "Get Headlines" button (which has the code in it from the NPRInfo class) to get the Titles and Descriptions of the articles to show up in the text box. Ignore the Keyword button for now, I will start working on that once I fix this problem. Can anybody please point out what I am doing wrong and how I can go about fixing it? Thanks for any and all help!

//import java.io.*;
//import java.io.BufferedReader;
//import java.io.InputStreamReader;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;


public class NPRNews extends JFrame implements ActionListener {
    JButton btnURL;
    JButton btnKW;
    JButton btnTD;
    JTextArea txtArea;
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static void main(String[] args) throws Exception {
        new NPRNews();
    }
    public NPRNews() throws Exception {
        super("NPR News");
        setupGUI();
        NPRInfo();
        registerListeners();
    }

    public void NPRInfo() throws Exception {
        try {
            DocumentBuilderFactory XmlBuilder = DocumentBuilderFactory.newInstance();
            DocumentBuilder xBuilder = XmlBuilder.newDocumentBuilder();
            Document xml = xBuilder.parse(new URL("http://www.npr.org/rss/rss.php?id=1001").openStream());
            if(xml==null)
                System.out.println("no XML DOM Document retrieved.");
                xml.getDocumentElement().normalize();

                NodeList nList = xml.getElementsByTagName("item");
            if(nList==null)
                System.out.println("No elements with tag name channel retrieved.");

            int temp = 0;
            for (temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE){
                    Element eElement = (Element) nNode;
                    txtArea.setText("Title : " + eElement.getElementsByTagName("title").item(0).getChildNodes().item(0).getNodeValue());
                    txtArea.setText("Description : "+ eElement.getElementsByTagName("description").item(0).getChildNodes().item(0).getNodeValue());
                    txtArea.setText("------------------------------------------------");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void setupGUI() {
        JPanel pnlCanvas = new JPanel();
        btnURL = new JButton("Connect to NPR");
        pnlCanvas.add(btnURL);
        btnKW = new JButton("NPR Keywords");
        pnlCanvas.add(btnKW);
        btnTD = new JButton("NPR Headlines");
        pnlCanvas.add(btnTD);
        txtArea = new JTextArea();
        pnlCanvas.add(txtArea);
        pnlCanvas.setLayout(new FlowLayout());

        Container mainPanel = this.getContentPane();
        mainPanel.add(pnlCanvas, BorderLayout.SOUTH);
        mainPanel.add(txtArea, BorderLayout.CENTER);


        this.setSize(500, 300);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void registerListeners(){
        btnURL.addActionListener(this);
        btnKW.addActionListener(this);
        btnTD.addActionListener(this);
    }
    public void openConnection() throws Exception{
        URL url = new URL("http://www.npr.org/rss/rss.php?id=1001");
        URLConnection connection = url.openConnection();
        connection.connect();
    }


    public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());
        if (e.getSource() == btnURL) {
            try {
                this.openConnection();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        if (e.getSource() == btnTD) {
            try {
                this.NPRInfo();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }
}

Upvotes: 0

Views: 776

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347234

Don't use JTextArea#setText, use JTextArea#append instead.

setText does just that, changes the text of the text area to what ever you specify, discarding what ever was previous applied to it.

Upvotes: 1

Related Questions