用java解析Ofx文件

发布于 2024-12-01 21:15:13 字数 1590 浏览 0 评论 0原文

我目前正在尝试用 java 读取 ofx 文件。 但我收到以下错误:未处理的异常类型 FileNotFoundException (对于第二行)。我正在使用 OFx4j。您能给我一些关于这方面的建议吗?

这是我到目前为止编写的代码:

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)
    {
    }

感谢您的评论,我做了一些更改:

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

但现在我得到了:

线程“main”java.lang.NoClassDefFoundError中的异常:net/n3/nanoxml/XMLParseException 在 OfxTest.afficherFichier(OfxTest.java:31) 在 OfxTest.main(OfxTest.java:20) 引起原因:java.lang.ClassNotFoundException:net.n3.nanoxml.XMLParseException 在 java.net.URLClassLoader$1.run(来源未知) 在 java.security.AccessController.doPrivileged(本机方法) 在 java.net.URLClassLoader.findClass(来源未知) 在 java.lang.ClassLoader.loadClass(来源未知) 在 sun.misc.Launcher$AppClassLoader.loadClass(来源未知) 在 java.lang.ClassLoader.loadClass(来源未知) ... 2 更多

我正在尝试弄清楚。我相信它找不到 XMLParseException。但我不确定。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮光沉寂 2024-12-08 21:15:13

您遇到的第二个问题:“Exception in thread "main" java.lang.NoClassDefFoundError: net/n3/nanoxml/XMLParseException”意味着您尚未包含此处的 NanoXML 库: http://devkix.com/nanoxml.php

您还需要 Apache Commons Logging 库,因为 NanoXML 似乎是依赖的关于这一点。此处提供:http://commons.apache.org/logging/download_logging.cgi

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

删除→记忆 2024-12-08 21:15:13

这意味着您没有捕获FileNotFoundException。另外,虽然这与您的错误消息无关,但作为最佳实践,您应该始终在finally块中关闭文件流,如下所示。也不需要对文件名执行 new String() 操作。

FileNotFoundException 添加此 catch 块:-

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

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();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文