Parsing Ofx file with java
I am currently trying to read an ofx file with java.
But I get the following error: Unhandled exception type FileNotFoundException
(for the 2nd line). I am using OFx4j. Could you please give me some tips on that one?
Here is the code I have written so far:
String filename=new String("C:\\file.ofx");
FileInputStream file = new FileInputStream(filename);
NanoXMLOFXReader nano = new NanoXMLOFXRead开发者_如何学Cer();
try
{
nano.parse(stream);
System.out.println("woooo It workssss!!!!");
}
catch (OFXParseException e)
{
}
Thanks for your comments, I made some changes:
String FILE_TO_READ = "C:\\file.ofx";
try
{
FileInputStream file = new FileInputStream(FILE_TO_READ);
NanoXMLOFXReader nano = new NanoXMLOFXReader();
nano.parse(file);
System.out.println("woooo It workssss!!!!");
}
catch (OFXParseException e)
{
System.out.println("Message : "+e.getMessage());
}
catch (Exception e1)
{
System.out.println("Other Message : "+e1.getMessage());
}
But now I am getting this:
Exception in thread "main" java.lang.NoClassDefFoundError: net/n3/nanoxml/XMLParseException at OfxTest.afficherFichier(OfxTest.java:31) at OfxTest.main(OfxTest.java:20) Caused by: java.lang.ClassNotFoundException: net.n3.nanoxml.XMLParseException at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 2 more
I am trying to figure it out. I believe it can't find the XMLParseException. But I am not sure.
The second problem that you're encountering: "Exception in thread "main" java.lang.NoClassDefFoundError: net/n3/nanoxml/XMLParseException" means that you haven't included the NanoXML library from here: http://devkix.com/nanoxml.php
You will also need the Apache Commons Logging library, as NanoXML appears to be dependent on this. Available here: http://commons.apache.org/logging/download_logging.cgi
This means that you are not catching FileNotFoundException
. Also although this is not relevant to your error message but as best practice you should always close you file stream in the finally block like I have below. There is also no need to do to new String() on the file name either.
Add this catch block for the FileNotFoundException
:-
String filename = "C:\\file.ofx";
FileInputStream file = null;
NanoXMLOFXReader nano = null;
try
{
file = new FileInputStream(filename);
nano = new NanoXMLOFXReader();
nano.parse(stream);
System.out.println("woooo It workssss!!!!");
}
catch (OFXParseException e)
{
e.printStackTrace();
throw e;
}catch (FileNotFoundException e){
e.printStackTrace();
throw e;
}finally{
if(file!=null){
file.close();
}
}
精彩评论