2014-01-09 117 views
0

我正在尝试创建一个xml文件阅读器。我在JFrame中使用eclipse制作了主要的xml文件,并编写了如下的文件读取器代码;java中的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 
+1

是否有任何特定的原因,你正试图建立一个XML阅读器?因为有很多不同的产品适合各种需求。 –

+0

@CeilingGecko事实上有......这是我的课程任务。如果我只能使用互联网上的许多xml读取器之一,那么为什么我会在这里请求关于我当前的xml读取器代码的指导? – MrShazzyMan

+0

那么什么是堆栈跟踪? –

回答

0

根据链接外,你缺少一些进口,如:

File cannot be resolved to a type缺少

import java.io.File; 

List cannot be resolved to a type不见了

import java.util.List; 

等 尝试为该例外中提及的所有类添加正确的导入。

而且,正如@Gaurav Joseph所提到的,您正在使用错误的方法来获取文件。

0

我想你理解错了再试

File file = fc.getSelectedFile() 
+0

我已经做了这个改变我的代码现在我得到了一个错误在行public static void main(String [] args){即使没有红色x旁边的代码行,这行代码甚至都不对。 @Gaurav – MrShazzyMan

+0

是的,有没有方法调用getSelected提到这个:http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html至于其余的错误使用进口' import java.io.File;'和'import java.util.List;'也可以从任何地方导入Student类代码。 –