01.dom4j 创建xml文档

来源:互联网 发布:51单片机自学笔记视频 编辑:程序博客网 时间:2024/06/06 04:02

这里面开始没搞懂的就是xml的头部信息,不知道怎么弄(请看Service)

2.service

    /**     * 导出xml报文文件内容.     * @param flag -- 默认是2,执行总账类方法     * @param sysdto -- 账套     * @param hsagency -- 单位     * @return     * @throws AppException -- 自定义异常     */    public String exportVchXml(String flag, NewFmAccountSystemTypeDTO sysdto, String hsagency,String startterm,             String endterm, HttpServletResponse response) throws AppException {        //System.out.println(doc.asXML().replaceAll("\n", ""));        String filepath = "";        if ("1".equals(flag)) { // 导出公共档案            filepath = exportBaseDataXml(sysdto, hsagency, response);        } else if ("2".equals(flag)) { // 导出总账类            filepath = exportSumVchXml(sysdto, hsagency,startterm,endterm, response);        } else {            throw new AppException("导出文件类型有误,请检查!");        }        return filepath;    }    /**     * 导出总账凭证类xml文件.     * @param sysdto -- 账套     * @param hsagency -- 单位     * @return     * @throws AppException     */    public String exportSumVchXml(NewFmAccountSystemTypeDTO sysdto, String hsagency, String startterm, String endterm, HttpServletResponse response) throws AppException {        String tempfilename = "总账类";        String acctsystype = sysdto.getAcctsystypeid()+""; //账套id        //期间查询条件待定        String termfilter = " ((term between " + startterm + " and " + endterm + ") or term = 0)";        int startterm1 = Integer.parseInt(startterm);        int endterm1  = Integer.parseInt(endterm);        //报文头信息        Document document = DocumentHelper.createDocument();        //根节点        Element rootElmt = document.addElement("总账","http://sxbw.audit.gov.cn/AccountingSoftwareDataInterfaceStandard/2010/PSGA/XMLSchema");        //命名空间        //xmlns:行政事业单位="http://sxbw.audit.gov.cn/xxxxXMLSchema"         //xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           rootElmt.addNamespace("行政事业单位", "http://sxbw.audit.gov.cn/xxxx/XMLSchema");        rootElmt.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");        //xsi:schemaLocation="http://sxbw.audit.gov.cn/AccountingSoftwareDataInterfaceStandard/2010/PSGA/XMLSchema 总账.xsd"        rootElmt.addAttribute("xsi:schemaLocation", "http://sxbw.audit.gov.cn/AccountingSoftwareDataInterfaceStandard/2010/PSGA/XMLSchema 总账.xsd");        /***总账基础信息**/        Element accttypeElement = rootElmt.addElement("总账基础信息");        accttypeElement.addElement("结构分隔符").setText("-");        accttypeElement.addElement("会计科目编号规则").setText(sysdto.getCodeformat());        accttypeElement.addElement("凭证头可扩展字段结构").setData(null);        accttypeElement.addElement("凭证头可扩展结构对应档案").setData(null);        accttypeElement.addElement("分录行可扩展字段结构").setData(null);        accttypeElement.addElement("分录行可扩展字段对应档案").setData(null);        /**报表集*/        Element reportElement = rootElmt.addElement("报表集");        reportElement.addElement("报表编号");        reportElement.addElement("报表名称");        reportElement.addElement("报表报告日");        reportElement.addElement("报表报告期");        reportElement.addElement("编制单位");        reportElement.addElement("货币单位");        /**报表项数据*/        Element reportItemElement = rootElmt.addElement("报表项数据");        reportItemElement.addElement("报表编号");        reportItemElement.addElement("报表项编号");        reportItemElement.addElement("报表项名称");        reportItemElement.addElement("报表项公式");        reportItemElement.addElement("报表项数值");        String filepath = ExpXmlUtil.expExcelTemplate(null, tempfilename,document, response);        return filepath;    }

3.DAO略

4.ExpxmlUtil

/** *  */package gov.mof.fasp.ifmis.acal.common.expxml;import gov.mof.framework.util.DateUtil;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.math.BigDecimal;import java.text.DecimalFormat;import javax.servlet.http.HttpServletResponse;import org.dom4j.Document;import org.dom4j.io.OutputFormat;import org.dom4j.io.XMLWriter;/** *一个将xml文件保存到服务器 *一个将xml以字符串格式输出到客户端 */public class ExpXmlUtil {    /**     * 构造方法设置为私有.     */    private ExpXmlUtil() {        super();    }    /**     * @param path --     * @param tmpname     * @param columns     * @param dataList     * @return     */    public static String expExcelTemplate(String path, String tmpname,Document document, HttpServletResponse response) {        if (tmpname == null) {            tmpname = "";        }        if (path == null || "".equals(path)) {            path = new File("").getAbsolutePath();            path = path.replaceAll("\\\\", "/");        }        path += "/cal/impexp/expfilebak/";        File f = new File(path);        f.mkdirs();        String filename = tmpname + ".xml";        path += filename;        // 封装xml报文数据        OutputFormat format = OutputFormat.createPrettyPrint();        format.setEncoding("utf-8");// 设置XML文件的编码格式        format.setNewLineAfterDeclaration(false);//声明之后不换行        //format.setSuppressDeclaration(true);// 不输出头信息        format.setIndent(false); //设置是否缩进        //format.setIndent(" "); //以空格方式实现缩进        format.setNewlines(true); //设置是否换行        XMLWriter writer = null;        try {            writer = new XMLWriter(new FileWriter(path), format);//写入指定的文件            writer.write(document);        } catch (IOException e) {            path="";            e.printStackTrace();        } finally {            try {                writer.close();            } catch (IOException e) {                e.printStackTrace();            }        }        return path;        /*ByteArrayOutputStream baos = new ByteArrayOutputStream();        OutputFormat format = OutputFormat.createPrettyPrint();        format.setEncoding("UTF-8");        format.setNewLineAfterDeclaration(false);//声明之后不换行        format.setNewlines(true); //设置是否换行        XMLWriter writer;        try {            writer = new XMLWriter(baos, format);            writer.write(document);            writer.flush();            writer.close();            String fileName = tmpname + DateUtil.getCurrentDateStr(DateUtil.C_DATA_PATTON_YYYYMMDD) + ".xml";            String dispositionHeader = "attachment;filename=" + new String(fileName.getBytes(), "iso8859-1");            // 发送中文文件名的响应头信息            response.setHeader("Content-Disposition", dispositionHeader);            response.setHeader("Content-Type", "application/x-msdownload; charset=gbk");            OutputStream out = response.getOutputStream();            out.write(baos.toByteArray());            out.flush();            out.close();        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        return null;*/    }    /**     * 格式化金额.     * @param amt --金额     * @return     */    public static String numFormat(String amt) {        DecimalFormat df = new DecimalFormat("#0.00");        BigDecimal amtbd = new BigDecimal(amt);        return df.format(amtbd);    }}

导出部分xml格式

<?xml version="1.0" encoding="utf-8"?><总账 xmlns="http://sxbw.audit.gov.cn/AccountingSoftwareDataInterfaceStandard/2010/PSGA/XMLSchema" xmlns:行政事业单位="http://sxbw.audit.gov.cn/AccountingSoftwareDataInterfaceStandard/2010/PSGA/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://sxbw.audit.gov.cn/AccountingSoftwareDataInterfaceStandard/2010/PSGA/XMLSchema 总账.xsd"><总账基础信息><结构分隔符>-</结构分隔符><会计科目编号规则>4-2-2-2-2</会计科目编号规则><凭证头可扩展字段结构/><凭证头可扩展结构对应档案/><分录行可扩展字段结构/><分录行可扩展字段对应档案/></总账基础信息><记账凭证类型><记账凭证类型编号>01</记账凭证类型编号><记账凭证类型名称>记账凭证</记账凭证类型名称><记账凭证类型简称></记账凭证类型简称></记账凭证类型><会计科目><科目编号>1001</科目编号><科目名称>库存现金</科目名称><科目级次>1</科目级次><科目类型>资产</科目类型><余额方向></余额方向></会计科目>
原创粉丝点击