Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JFileChooser GUI可以正确打开文件XML,但随后无法打开';I don’我没看错_Java_Xml_Swing_User Interface - Fatal编程技术网

Java JFileChooser GUI可以正确打开文件XML,但随后无法打开';I don’我没看错

Java JFileChooser GUI可以正确打开文件XML,但随后无法打开';I don’我没看错,java,xml,swing,user-interface,Java,Xml,Swing,User Interface,我正在尝试做以下练习: 编写一个图形程序,允许用户从 使用文件选择器GUI组件的磁盘。文件打开后,其内容 显示在java文本区域GUI组件中。可以创建一个示例XML文件 在moodle'Books.xml'上找到 它可以工作,但是xml文档没有被正确读取。出现了。 见下图: 1-这是我的类ReadFilewithGui: package Tut5Ex2Part1; import java.awt.EventQueue; import javax.swing.JFrame; import ja

我正在尝试做以下练习:

编写一个图形程序,允许用户从 使用文件选择器GUI组件的磁盘。文件打开后,其内容 显示在java文本区域GUI组件中。可以创建一个示例XML文件 在moodle'Books.xml'上找到

它可以工作,但是xml文档没有被正确读取。出现了。 见下图:

1-这是我的类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);
}
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");
        }

    }

}
这是我的OpenFileClass:

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);
}
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");
        }

    }

}
这是我的XML文件

<?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>

马修·甘巴德拉
XML开发人员指南
计算机
44.95
2000-10-01
深入了解如何使用XML创建应用程序。
欢迎任何帮助!谢谢:)

您可以使用一个解决方案。您还可以使用一个标签来避免打印标签

在代码中,您可以将
replaceAll
添加到以下内容

sb.append(input.nextLine());

解决方案
这是我的xml文件。Gambardella,Matthew XML开发人员指南计算机44.95 2000-10-01使用XML创建应用程序的深入介绍。嘿,尚塔尔,你这是什么意思。XML只是文本。是否只解析它并读取XML属性值??顺便说一句,你的代码在我的机器上运行良好。如果你能正确地解释你需要什么样的澄清,这将对你大有帮助。谢谢你们,伙计们!所有这些都有帮助