2

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

好的,但我该怎么做?

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

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

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

请不要抨击我,我正在使用 playframework 学习 java,我非常感谢能够解析这些 ofx 文件。

提前致谢。

4

2 回答 2

5

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.

于 2010-02-08T17:13:43.087 回答
2

对于那些在我无法从 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();
  }
}
于 2010-11-08T22:18:25.567 回答