OFX4J

OFX Client

Lookup Your Financial Institution

You have to first find the financial institution you want to interface with. Use an implementation of net.sf.ofx4j.client.FinancialInstitutionDataStore to list the available financial institution data. Or, construct your own data if your FI data isn't available.

The two default implementations of FinancialInstitutionDataStore are LocalResourceFIDataStore and OFXHomeFIDataStore . The former looks up the data store list from a local resource (default is the list at "/META-INF/ofx4j/institutions.xml" on the classpath; a default one is packaged with the jar). The latter screen-scrapes this page at OFX Home , so it's subject to break depending on how often the format of that page changes.

Anyway, once you have a reference to the FinancialInstitutionData you want, you can now use it to interface with your FI.

Interfacing With Your Financial Institution

FinancialInstitutionData data = ...;
FinancialInstitutionService service
  = new FinancialInstitutionServiceImpl();
FinancialInstitution fi = service.getFinancialInstitution(data);

// read the fi profile (note: not all institutions
// support this, and you normally don't need it.)
FinancialInstitutionProfile profile = fi.readProfile();

//get a reference to a specific bank account at your FI
BankAccountDetails bankAccountDetails
  = new BankAccountDetails();

//routing number to the bank.
bankAccountDetails.setRoutingNumber("11111111");
//bank account number.
bankAccountDetails.setAccountNumber("1234-5678");
//it's a checking account
bankAccountDetails.setAccountType(AccountType.CHECKING);

BankAccount bankAccount
  = fi.loadBankAccount(bankAccountDetails, "username", "password");

//read the statement (transaction details, etc.)
// for a given time period.
Date startDate = ...;
Date endDate = ...;
AccountStatement statement
  = bankAccount.readStatement(startDate, endDate);

// get a reference to a specific credit card
// account at your FI
CreditCardAccountDetails ccDetails
  = new CreditCardAccountDetails();
ccDetails.setAccountNumber("1234-567890-1111");
CreditCardAccount ccAccount
  = fi.loadCreditCardAccount(ccDetails, "username", "password");

// read the statement (transaction details, etc.)
// for a given time period.
Date startDate = ...;
Date endDate = ...;
AccountStatement statement
  = ccAccount.readStatement(startDate, endDate);

OFX Server

You can use OFX4J to implement your own OFX server. To do so, you must implement the net.sf.ofx4j.server.OFXServer interface. Once an adequate implementation of this interface is exists, you can use the net.sf.ofx4j.server.OFXServlet in your J2EE application, passing the name of the OFXServer implementation in the "ofx-server-class" init parameter.

<web-app>

  ...

  <servlet>
    <servlet-name>OFXServlet</servlet-name>
    <servlet-class>net.sf.ofx4j.server.OFXServlet</servlet-class>
    <init-param>
      <param-name>ofx-server-class</param-name>
      <param-value>fqn.of.the.OFXServerImpl</param-value>
    </init-param>
    <!-- uncomment to use OFX v2
    <init-param>
      <param-name>ofx-version</param-name>
      <param-value>2</param-value>
    </init-param>
    -->
  </servlet>

  ...

  <servlet-mapping>
    <servlet-name>OFXServlet</servlet-name>
    <url-pattern>/ofx</url-pattern>
  </servlet-mapping>

  ...

</web-app>

OFX I/O

Reading OFX data

OFX4J boasts a very fast, event-based OFX reading mechanism. If you've got a file or other stream resource, you can read it using an instance of net.sf.ofx4j.io.OFXReader . We suggest the net.sf.ofx4j.io.nanoxml.NanoXMLOFXReader , which uses utilities provided by the NanoXML project. This reader will detect the version of OFX being used (version 1 or 2) and parse it accordingly.

Or you want to unmarshal the OFX directly to a Java object, use the net.sf.ofx4j.io.AggregateUnmarshaller .

Writing OFX data

You can also write OFX data using an instance of net.sf.ofx4j.io.OFXWriter . There exists an implementation for writing to OFX version 1 and an implementation for writing to OFX version 2 . If you want to marshal from Java objects, use the net.sf.ofx4j.io.AggregateMarshaller