2010-02-08 93 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(); 
    } 
}