简体   繁体   中英

XML file reader in java

I'm trying to create a xml file reader. I've made the main xml file using eclipse within a JFrame and written the file reader code as below;

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

I'm getting an error on the bit where it says;

File file = fc.getSelected();

The error I'm getting is this;

http://gyazo.com/10d739192c178e04a085bd392e93139b

我想你试错了

File file = fc.getSelectedFile()

According to the exception in link, you are missing some imports, eg:

File cannot be resolved to a type is missing

import java.io.File;

List cannot be resolved to a type is missing

import java.util.List;

etc. Try to add correct import for all classes mentioned in that exception.

And, as @Gaurav Joseph mentioned, you are using wrong method to get file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM