欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JDOM创建xml解决头没有standalone属性

程序员文章站 2022-05-28 10:42:05
...

最近接手的新项目有个需求要求将需要的表数据将字段作为节点生成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>
相关标签: jdom xml