JAVA编码(15)——使用FRAMEMARK创建XML

首先需要导入freemarker-2.3.19.jar包

然后穿件一个xml文件模板

 

再次:

Map root= new HashMap();
    for (int i = 0; i < listborrownum.size(); i++) {//生成借的凭证
   voucherborrow = new VoucherEntrySchema();
   voucherborrow.setId(i+1);
   voucherborrow.setPayDate(payDate);
   voucherborrow.setObjectinfo(listborrownum.get(i).getObjectinfo());
   voucherborrow.setPayMoney1(listborrownum.get(i).getPayMoney1());
   listborrow.add(voucherborrow);
    }
    for (int i = 0; i < listcreditnum.size(); i++) {//生成贷的凭证对象
    vouchercredit = new VoucherEntrySchema();
    vouchercredit.setId(i+2);
    vouchercredit.setPayDate(payDate);
    vouchercredit.setObjectinfo(listcreditnum.get(i).getObjectinfo());
    vouchercredit.setPayMoney2(listcreditnum.get(i).getPayMoney2());
    listcredit.add(vouchercredit);
    }
    root.put("currentMills", "ZJ"+System.currentTimeMillis());//当前时间的毫秒数
    root.put("currentDate", currentDate);//当前的日期
    root.put("number", number);//生成 entry 的总记录数
    root.put("entryborrow", listborrow);
    root.put("entrycredit", listcredit);
   
    Configuration config = new Configuration();
    URL url=Thread.currentThread().getContextClassLoader().getResource("CreateVoucher.ft");
    temp=url.getFile();
    temp=temp.substring(1,temp.length());
    temp=temp.substring(0,temp.lastIndexOf("/"));
    config.setDirectoryForTemplateLoading(new File(temp));
    config.setObjectWrapper(new DefaultObjectWrapper());
    Template t = config.getTemplate("CreateVoucher.ft");
    String currentTime = current.replaceAll("-","");
    InputStream in = null;
    FileOutputStream fos = null;
    File file = null;
    String voucher1 = LoadLocationProperties.getPropertiesValue("/location.properties", "voucher1");
    try {
     System.out.println(voucher1);
     file=new File(voucher1+"999999999_"+currentTime+"_LNAE_"+System.currentTimeMillis()+"_"+(Math.random()+"").substring(2)+".xml");
     file.getParentFile().mkdirs();
     if(!file.exists()){
      file.createNewFile();
     }
     fos = new FileOutputStream(file);
     PrintWriter writer = new PrintWriter(fos);
     t.process(root, writer);
     listborrow.remove(voucherborrow);
     listcredit.remove(vouchercredit);
    } catch (Exception e) {
     e.printStackTrace();
    }finally{
     fos.flush();
     fos.close();

  

转载于:https://www.cnblogs.com/xushuyi/articles/4394549.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值