0

I am trying to do the following exercise:

Write a graphics program that allows a user to load an XML file from the disk using a file chooser GUI component. Once the file is open, its content is displayed in a java text area GUI component. A sample XML file can be found on moodle ‘Books.xml’.

It works but the xml document is not read correctly. The <> appear. See picture below :

Image

1- This is my class ReadFilewithGui:

package Tut5Ex2Part1;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;

    public class ReadFileWithGui {

         private JFrame frame;

/**
 * Launch the application.
 */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                 public void run() {
                     try {
                ReadFileWithGui window = new ReadFileWithGui();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public ReadFileWithGui() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JTextArea textArea = new JTextArea();
    textArea.setBounds(56, 73, 346, 100);
    frame.getContentPane().add(textArea);

    JButton btnGetFile = new JButton("Get file");
    btnGetFile.setFont(new Font("Lantinghei TC", Font.PLAIN, 13));
    btnGetFile.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            OpenFile of= new OpenFile();

            try{
                of.PickMe();
            }
            catch (Exception e){
                e.printStackTrace();
            }
            textArea.setText(of.sb.toString());
        }
    });
    btnGetFile.setBounds(175, 219, 117, 29);
    frame.getContentPane().add(btnGetFile);
}

This is my OpenFileClass:

package Tut5Ex2Part1;
import java.util.Scanner;

import javax.swing.JFileChooser;

public class OpenFile{


    JFileChooser fileChooser = new JFileChooser();
    StringBuilder sb = new StringBuilder();
    //String builder is not inmutable

    public void PickMe() throws Exception{
        //Opens open file dialog 
        if(fileChooser.showOpenDialog(null)== JFileChooser.APPROVE_OPTION) {
            //returns selected file. We are using the GUI file chooser 
            java.io.File file = fileChooser.getSelectedFile();
            //creates a scanner for the file
            Scanner input = new Scanner(file);

            while(input.hasNext()){
                sb.append(input.nextLine());
                sb.append("\n");
            }
            //closes scanner when we are finished
            input.close();
        }
        else{
            //if file not selected. example cancel botton.
            sb.append("You have not selected a file");
        }

    }

}

This is my XML file

<?xml version="1.0"?>
<catalog>
    <book id="bk101">
        <author>Gambardella, Matthew</author>
        <title>XML Developer's Guide</title>
        <genre>Computer</genre>
        <price>44.95</price>
        <publish_date>2000-10-01</publish_date>
        <description>An in-depth look at creating applications with XML.</description>
    </book>
</catalog>

Any help is welcome! Thanks :)

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
CHANTAL COX
  • 73
  • 2
  • 13
  • this is my xml file. Gambardella, Matthew XML Developer's Guide Computer 44.95 2000-10-01 An in-depth look at creating applications with XML. – CHANTAL COX Nov 18 '15 at 18:51
  • Hey Chantal what do you mean by <>. XML is simply text. Do you want to parse it and read the XML attribute values only ?? BTW your code is working well on my machine. If you are able to explain properly what clarification you need it will be great – Acewin Nov 18 '15 at 19:14
  • This may help you. http://www.mkyong.com/java/jaxb-hello-world-example/ – Acewin Nov 18 '15 at 19:18
  • thanks you guyys! All of this helped – CHANTAL COX Dec 09 '15 at 13:20

1 Answers1

0

You could use a Regex to solve this. You can also use an XML parser to avoid printing tags.

Within your code, you could add a replaceAll to the following

sb.append(input.nextLine());

Solution

sb.append(input.nextLine().replaceAll("<[^>]+>", ""));

Output

Gambardella, MatthewXML 
Developer's Guide
Computer
44.95
2000-10-01
An in-depth look at creating applications with XML.
Community
  • 1
  • 1
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89