How to parse a malformed xml (ofx) with ofx4j?

独自空忆成欢 提交于 2019-11-29 05:17:48

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.

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();
  }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!