2010-02-08 91 views
2

我正在努力嘗試使用以下庫:ofx4j。但是與解析一個ofx文件相關的文檔有點簡潔。它說:如果你有一個文件或其他流資源,你可以閱讀它使用net.sf.ofx4j.io.OFXReader的實例如何使用ofx4j解析格式錯誤的xml(ofx)?

好吧,但我該怎麼辦?

它還聲明以下內容:如果要將OFX直接解組爲Java對象,請使用net.sf.ofx4j.io.AggregateUnmarshaller。

很好,但這對我來說有點複雜。有沒有什麼明顯的我錯過了?當我嘗試使用unmarshaller時,它會要求我實現一個接口。

有人能指點我一個在線資源來解釋我缺少的位嗎?或者說最好的,你從前面的陳述和解讀者和解組者那裏得到了什麼?

請不要打擾我,我正在學習與playframework的Java和我真的很感激能夠解析這些ofx文件。

在此先感謝。

回答

5

我看不到一個普通的舊教程,但在test目錄中有示例代碼,它說明了OFXReaderAggregateUnmarshaller

短語「net.sf.ofx4j.io.OFXReader一個實例」指的是公知的實現類」,如NanoXMLOFXReader之一,這是tested here一種用於AggregateUnmarshaller測試是here

APImail檔案是良好的資源,也它看起來像很多institutions參與。

+0

非常感謝你,你完全是我的一天...我很慚愧,我錯過了單元測試。 非常感謝您的指導。 – mrburns 2010-02-08 23:49:09

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