Search code examples
javaparsingofx

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 NanoXMLOFXReader();

    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.


Solution

  • 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