JAVA——JDOM生成xml,设置standalone属性

最近接手的新项目有个需求要求将需要的表数据将字段作为节点生成xml下载下来,把自己遇到的坑和大家分享一下。
话不多说,直接进入主题!

实现

引入jar包

<dependency>
	    <groupId>org.jdom</groupId>
	    <artifactId>jdom</artifactId>
	    <version>1.1.3</version>
</dependency>

实现代码

public void generateXML(@PathVariable("sourceId") String sourceId,HttpServletRequest request,HttpServletResponse response){
		DataSource dataSource = sourceManager.getEntityInfoByPk(Long.valueOf(sourceId));
		//col数据
		List<DataSourceColInfo> colSource = sourceManager.getColBySource(Long.valueOf(sourceId));
		
		//设置日期格式
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		//获取当前系统时间
		String time = df.format(new Date());
		//文件名  =  当前时间+区划代码+数量+表名
		String fileName = time+"-13000-1-"+dataSource.getTableName().toLowerCase();
		//文件类型
        String fileType = "xml";
        
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + "." + fileType);
        response.setContentType("multipart/form-data");
        //2.将数据转为xml格式的字符串
        //根节点
        Element root = new Element((dataSource.getTableName()+"S").toLowerCase());
        Document document = new Document(root);
        //父节点
        Element element = new Element(dataSource.getTableName().toLowerCase());
        for (DataSourceColInfo dataSourceColInfo : colSource) {
        	//子节点
			element.addContent(new Element(dataSourceColInfo.getColNameEn().toLowerCase()).setText("1"));
		}
        root.addContent(element);
        
        //使xml文件 缩进效果
        Format format = Format.getPrettyFormat(); 
        format.setEncoding("gbk");
        
        // 创建XMLOutputter的对象
        XMLOutputter outputer = new XMLOutputter(format);
        String result = outputer.outputString(document);
        try {
            //3.将内容转为byte[]格式
            byte[] data = result.getBytes("gbk");
            
            //4.将内容写入响应流
            OutputStream out = response.getOutputStream();
            out.write(data);
            
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
	}

到这里一个xml就下载下来了,结果如下

<?xml version="1.0" encoding="gbk"?>
<jgsy_commons>
  <jgsy_common><!--表名-->
    <jgsy_code>1</jgsy_code>
    <jgsy_parent_code>1</jgsy_parent_code>
    <dep_code>1</dep_code>
    <jgsy_name>1</jgsy_name>
    <jgsy_type>1</jgsy_type>
    <jgsy_system_code>1</jgsy_system_code>
    <jgsy_states>1</jgsy_states>
    <jgsy_code_order>1</jgsy_code_order>
    <chexiao_time>1</chexiao_time>
    <chexiao_wh>1</chexiao_wh>
    <jgsy_ifvertical>1</jgsy_ifvertical>
    <jgsy_ifzfjg>1</jgsy_ifzfjg>
    <sfskfq>1</sfskfq>
    <unify_code>1</unify_code>
  </jgsy_common>
</jgsy_commons>

但是并没有达到要求格式

<?xml version="1.0" encoding="gbk" standalone="yes"?>

jdom不支持standalone,那如何添加standalone在xml头里面了?
方法肯定是有的,重写XMLOutputter的printDeclaration方法

public class StandaloneXML extends XMLOutputter{
	private Format userFormat = Format.getRawFormat();
    protected static final Format preserveFormat = Format.getRawFormat();
    protected Format currentFormat = this.userFormat;
   
    public StandaloneXML() {
        super();
    }

    public StandaloneXML(Format format) {
        super(format);
    }

    public StandaloneXML(XMLOutputter that) {
        super(that);
    }

    protected void printDeclaration(Writer out, Document doc, String encoding)
            throws IOException {
        if (!(this.userFormat.getOmitDeclaration())) {
            out.write("<?xml version=\"1.0\"");
            if (!(this.userFormat.getOmitEncoding())){
                out.write(" encoding=\"" + encoding + "\"");
            }
            out.write(" standalone=\"yes\"");
            out.write("?>");

            out.write(currentFormat.getLineSeparator());
        }
    }
}

然后在controller层掉用

// 创建XMLOutputter的对象
XMLOutputter outputer = new XMLOutputter(format);
String result = outputer.outputString(document);

换成

//重写了printDeclaration方法,解决xml生成时没有standalone属性
StandaloneXML cxXmlOut = new StandaloneXML(format);
String result = cxXmlOut.outputString(document);

到这里就达到了格式要求

<?xml version="1.0" encoding="gbk" standalone="yes"?>
<jgsy_commons>
  <jgsy_common><!--表名-->
    <jgsy_code>1</jgsy_code>
    <jgsy_parent_code>1</jgsy_parent_code>
    <dep_code>1</dep_code>
    <jgsy_name>1</jgsy_name>
    <jgsy_type>1</jgsy_type>
    <jgsy_system_code>1</jgsy_system_code>
    <jgsy_states>1</jgsy_states>
    <jgsy_code_order>1</jgsy_code_order>
    <chexiao_time>1</chexiao_time>
    <chexiao_wh>1</chexiao_wh>
    <jgsy_ifvertical>1</jgsy_ifvertical>
    <jgsy_ifzfjg>1</jgsy_ifzfjg>
    <sfskfq>1</sfskfq>
    <unify_code>1</unify_code>
  </jgsy_common>
</jgsy_commons>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值