簡體   English   中英

Java中的XML文件閱讀器

[英]XML file reader in java

我正在嘗試創建xml文件閱讀器。 我已經在JFrame中使用eclipse制作了主要的xml文件,並編寫了如下文件閱讀器代碼;

public xml() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 486, 533);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JButton btnopen = new JButton("Open");
btnopen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

int returnVal = fc.showOpenDialog(contentPane);

if (returnVal == JFileChooser.APPROVE_OPTION) {
try
{
File file = fc.getSelected();
textFirst.setText(file.getAbsolute("XML", "xml"));

StaxParser read = new StaxParser();
List<Student> readStudents = read.readStudents(file.getAbsolutePath());
for (Student student : readStudents) {
textOutput.append(student+"\n\n");
}
}
catch (Exception e) {
//TODO Auto-generated catch block
e.printStackTrace();
textOutput.append("\nError");
}
} else {
textOutput.setText("user cancelled operation");
}
}
});

我在說的地方有點錯誤;

File file = fc.getSelected();

我得到的錯誤是這個;

http://gyazo.com/10d739192c178e04a085bd392e93139b

我想你試錯了

File file = fc.getSelectedFile()

根據鏈接中的異常,您缺少一些導入,例如:

File cannot be resolved to a type為缺少File cannot be resolved to a type

import java.io.File;

List cannot be resolved to a type為缺少List cannot be resolved to a type

import java.util.List;

等等。嘗試為該異常中提到的所有類添加正確的導入。

而且,正如@Gaurav Joseph所提到的,您正在使用錯誤的方法來獲取文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM