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

你可能感兴趣的:(企业应用)