dom4j生成XML文件

最近项目需要生成NC用友凭证,使用到

/***
* 生成凭证的XML文件
* @param logonUser
* @param list
*/
public void createXML(Map param, List list)
{
if(null == list)
throw new NullPointerException();
DocumentFactory f=new DocumentFactory();
Document doc=f.createDocument();//文档对象
//在文档对象中添加一棵树
Element root=doc.addElement("ufinterface");

root.addAttribute("billtype", "gl");
root.addAttribute("codeexchanged", "y");
root.addAttribute("docid","989898989898");
root.addAttribute("proc", "add");
root.addAttribute("receiver", "0001");
root.addAttribute("roottag", "voucher");
root.addAttribute("sender", "THDH");

Element voucher=root.addElement("voucher");//给根添加一个凭证
voucher.addAttribute("id", ((Voucher)list.get(0)).getVoucherUniqueId()); //凭证添加一个唯一ID属性

Element voucherHead= voucher.addElement("voucher_head"); //给凭证添加一个头元素

Element company = voucherHead.addElement("company"); //添加公司子元素
company.setText(((Voucher)list.get(0)).getCompany()); //给公司子元素设定内容

Element voucherType = voucherHead.addElement("voucher_type"); //添加凭证子元素
voucherType.setText("记账凭证"); //给凭证子元素设定内容

Element fiscalYear = voucherHead.addElement("fiscal_year"); //添加凭证子元素
fiscalYear.setText(((Voucher)list.get(0)).getFiscalYear()); //给凭证子元素设定内容

Element accountingPeriod = voucherHead.addElement("accounting_period"); //添加凭证子元素
accountingPeriod.setText(((Voucher)list.get(0)).getAccountingPeriod()); //给凭证子元素设定内容

Element voucherId = voucherHead.addElement("voucher_id"); //添加凭证子元素
voucherId.setText("0"); //给凭证子元素设定内容


Element voucherBody = voucher.addElement("voucher_body");
for(int k=0; k<list.size(); k++){
Voucher voucherTemp = (Voucher)list.get(k);
Element entry = voucherBody.addElement("entry");

Element entryId = entry.addElement("entry_id"); //添加凭证子元素
entryId.setText(voucherTemp.getEntryId()); //给凭证子元素设定内容

Element accountCode = entry.addElement("account_code"); //添加凭证子元素
accountCode.setText(voucherTemp.getAccountCode()); //给凭证子元素设定内容

Element abstracts = entry.addElement("abstract"); //添加凭证子元素
abstracts.setText(voucherTemp.getAbstracts()); //给凭证子元素设定内容

Element settlement = entry.addElement("settlement"); //添加凭证子元素
settlement.setText(""); //给凭证子元素设定内容

Element documentId = entry.addElement("document_id"); //添加凭证子元素
documentId.setText(""); //给凭证子元素设定内容

Element naturalDebitCurrency = entry.addElement("natural_debit_currency"); //添加凭证子元素
naturalDebitCurrency.setText(voucherTemp.getNaturalDebitCurrency().toString()); //给凭证子元素设定内容

Element creditQuantity = entry.addElement("credit_quantity"); //添加凭证子元素
creditQuantity.setText(voucherTemp.getCreditQuantity().toString()); //给凭证子元素设定内容

Element primaryCreditAmount = entry.addElement("primary_credit_amount"); //添加凭证子元素
primaryCreditAmount.setText(voucherTemp.getPrimaryCreditAmount().toString()); //给凭证子元素设定内容

Element secondaryCreditAmount = entry.addElement("secondary_credit_amount"); //添加凭证子元素
secondaryCreditAmount.setText(voucherTemp.getSecondaryCreditAmount().toString()); //给凭证子元素设定内容

Element naturalCeditCurrency = entry.addElement("natural_credit_currency"); //添加凭证子元素
naturalCeditCurrency.setText(voucherTemp.getNaturalCreditCurrency().toString()); //给凭证子元素设定内容

Element billType = entry.addElement("bill_type"); //添加凭证子元素
billType.setText(""); //给凭证子元素设定内容

Element billId = entry.addElement("bill_id"); //添加凭证子元素
billId.setText(""); //给凭证子元素设定内容

Element billDate = entry.addElement("bill_date"); //添加凭证子元素
billDate.setText(""); //给凭证子元素设定内容

//辅助核算有子项目
Element auxiliaryAccounting = entry.addElement("auxiliary_accounting"); //添加凭证子元素
Element item = auxiliaryAccounting.addElement("item");
item.addAttribute("name","网点项目辅助核算");
item.setText(voucherTemp.getAuxiliaryAccounting1());

Element detail = entry.addElement("detail"); //添加凭证子元素
detail.setText(""); //给凭证子元素设定内容

}
try{
//获取应用程序的相对路径
String path = getRequest().getSession().getServletContext().getRealPath("voucher");
// path = path.concat("\\WebContent\\voucher\\");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String busiDate = formatter.format(((Voucher) list.get(0))
.getBusiDate());
OrgBranch orgBranch = (OrgBranch) baseDAO.queryByPK(
OrgBranch.class, param.get("branchId").toString());
File file = new File(path+ "\\" + busiDate+"\\"
+ orgBranch.getBranchCode());
file.mkdirs();
OutputStream os = new FileOutputStream(file
+ "\\" + ((Voucher) list.get(0)).getVoucherUniqueId().toString()
+ ".xml");
logger.debug(file
+ "\\" + ((Voucher) list.get(0)).getVoucherUniqueId().toString()
+ ".xml");
// xml输出的格式
OutputFormat format = new OutputFormat();
format.setEncoding("UTF-8");
format.setIndent(true);// 是否缩进
format.setIndentSize(2);
format.setNewlines(true);

XMLWriter writer = new XMLWriter(os, format);// 创建一个专门书写xml的一个书写器
writer.write(doc);// 把包含数的doc对象写到xml文件中
// 关闭资源
writer.close();
os.close();


}catch(Exception e) {
e.printStackTrace();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值