【问题标题】:How to parse a malformed xml (ofx) with ofx4j?如何使用 ofx4j 解析格式错误的 xml (ofx)?
【发布时间】:2011-01-14 08:43:29
【问题描述】:

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

好的,但是我该怎么做?

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

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

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

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

提前致谢。

【问题讨论】:

    标签: java xml ofx


    【解决方案1】:

    我没有看到简单的旧教程,但在 test 目录中有示例代码,说明了 OFXReaderAggregateUnmarshaller

    短语“net.sf.ofx4j.io.OFXReader 的实例”表示已知的实现类之一”,例如NanoXMLOFXReader,即tested hereAggregateUnmarshaller 的测试是here

    APImail 档案也是很好的资源。看起来有很多institutions 参与。

    【讨论】:

    • 非常感谢你让我很开心......我很惭愧我错过了单元测试。非常感谢您的指导。
    【解决方案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();
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-05-20
      • 1970-01-01
      • 1970-01-01
      • 2010-11-03
      • 1970-01-01
      • 1970-01-01
      • 2010-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多