如何使用 ofx4j 解析格式错误的 xml (ofx)?

发布于 2024-08-20 19:46:29 字数 508 浏览 10 评论 0原文

我拼命尝试使用以下库:ofx4j。但是与解析 ofx 文件相关的文档有点精简。它说:如果您有文件或其他流资源,您可以使用 net.sf.ofx4j.io.OFXReader 的实例来读取它,

但是我该怎么办?

它还说明了以下内容:如果要将 OFX 直接解组为 Java 对象,请使用 net.sf.ofx4j.io.AggregateUnmarshaller。

好吧,但这对我来说有点复杂。有什么明显的事情我错过了吗?当我尝试使用解组器时,它要求我实现一个接口。

有人可以向我指出一个在线资源来解释我所缺少的部分吗?或者最好的是,您从前面有关 ofxreader 和 unmarshaller 的陈述中理解了什么?

请不要攻击我,我正在使用 playframework 学习 java,我真的很高兴能够解析这些 ofx 文件。

提前致谢。

i am desperatly trying to use the following library : ofx4j. But the documentation relative to parsing an ofx file is a bit lite. It says : If you've got a file or other stream resource, you can read it using an instance of net.sf.ofx4j.io.OFXReader

Ok but how do i do ?

It also states the following: if you want to unmarshal the OFX directly to a Java object, use the net.sf.ofx4j.io.AggregateUnmarshaller.

Fine, but that's a bit complicated for me. Is there something obvious that i missed ? When i try to use the unmarshaller, it asks me to implement an interface.

Could someone point me to an online resource explaining the bits that i am missing ? Or the best, what do you understand from the previous statements relative to the ofxreader and the unmarshaller ?

Please, don't bash me, I am learning java with the playframework and i would really appreciate to be able to parse those ofx files.

thanks in advance.

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

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

发布评论

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

评论(2

莳間冲淡了誓言ζ 2024-08-27 19:46:29

我没有看到简单的旧教程,但 test 目录,用于说明 OFXReaderAggregateUnmarshaller

短语“net.sf.ofx4j 的实例。 io.OFXReader“表示已知的实现类之一”,例如NanoXMLOFXReader,即 在这里测试AggregateUnmarshaller此处

API 和 邮件 档案也是很好的资源。看起来有很多机构参与。

I don't see a plain old tutorial, but there's sample code in the test directory that illustrates OFXReader and AggregateUnmarshaller.

The phrase "an instance of net.sf.ofx4j.io.OFXReader" means one of the known implementing classes", such as NanoXMLOFXReader, which is tested here. A test for AggregateUnmarshaller is here.

The API and mail archives are good resources, too. It looks like a lot of institutions participate.

少年亿悲伤 2024-08-27 19:46:29

对于那些像我一样当我无法从 AggregateUnmarshaller 获得预期结果时偶然发现这一点的人...这是一个示例。

//Using a multipart file, but using a regular file is similar.
public void parse(MultipartFile file) throws IOException {
  //Use ResponseEnvelope to start.
  AggregateUnmarshaller<ResponseEnvelope> unmarshaller = new AggregateUnmarshaller<ResponseEnvelope>(
    ResponseEnvelope.class);

  try {
    ResponseEnvelope envelope = unmarshaller.unmarshal(file.getInputStream());
    //Assume we are just interested in the credit card info.  Make sure to cast.
    CreditCardResponseMessageSet messageSet = (CreditCardResponseMessageSet) envelope
      .getMessageSet(MessageSetType.creditcard);

    List<CreditCardStatementResponseTransaction> responses = messageSet.getStatementResponses();
    for (CreditCardStatementResponseTransaction response : responses) {
      CreditCardStatementResponse message = response.getMessage();
      String currencyCode = message.getCurrencyCode();
      List<Transaction> transactions = message.getTransactionList().getTransactions();
      for (Transaction transaction : transactions) {
        System.out.println(transaction.getName() + " " + transaction.getAmount() + " "
          + currencyCode);
      }
    }
  }
  catch (OFXParseException e) {
    e.printStackTrace();
  }
}

For those that stumble on this like I did when I couldn't get the expected results from the AggregateUnmarshaller... Here is an example.

//Using a multipart file, but using a regular file is similar.
public void parse(MultipartFile file) throws IOException {
  //Use ResponseEnvelope to start.
  AggregateUnmarshaller<ResponseEnvelope> unmarshaller = new AggregateUnmarshaller<ResponseEnvelope>(
    ResponseEnvelope.class);

  try {
    ResponseEnvelope envelope = unmarshaller.unmarshal(file.getInputStream());
    //Assume we are just interested in the credit card info.  Make sure to cast.
    CreditCardResponseMessageSet messageSet = (CreditCardResponseMessageSet) envelope
      .getMessageSet(MessageSetType.creditcard);

    List<CreditCardStatementResponseTransaction> responses = messageSet.getStatementResponses();
    for (CreditCardStatementResponseTransaction response : responses) {
      CreditCardStatementResponse message = response.getMessage();
      String currencyCode = message.getCurrencyCode();
      List<Transaction> transactions = message.getTransactionList().getTransactions();
      for (Transaction transaction : transactions) {
        System.out.println(transaction.getName() + " " + transaction.getAmount() + " "
          + currencyCode);
      }
    }
  }
  catch (OFXParseException e) {
    e.printStackTrace();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文