xmlWriter 以UTF-8格式写xml问题
dom4j中的XMLWriter提供以下几种构造方法:
XMLWriter() XMLWriter(OutputFormat format) XMLWriter(OutputStream out) XMLWriter(OutputStream out, OutputFormat format) XMLWriter(Writer writer) XMLWriter(Writer writer, OutputFormat format)
最简单常用的可能是new XMLWriter(new FileWriter(...))这样的形式。可如果你一旦这么用,就会造成编码问题。由于dom4j对于文件编码的选择是用java本身类的处理方式(可以从源码看到),这么写就是采用FileWriter的处理方式,而FileWriter是不提供对编码的处理的。于是会调用系统自身的编码,比如用中文操作系统,编码方式就是gbk,但是它默认的在文件头写上<?xml version="1.0" encoding="UTF-8"?>。
也就是说,他以当前操作系统的编码保存文件,并且竟然自动添加文件头为"utf-8"格式,这会导致很多程序无法读取正确编码,而且具有很差的移植性(比如在windows下开发,放到linux服务器下跑,毕竟一般linux服务器默认local都是utf-8)。
解决途径一:
使用new XMLWriter(new FileOutputStream(...))方法
这样做,因为dom4j默认使用utf-8编码,即xml文件头默认编码方式,并且内容也会使用utf-8保存,这样可以做到一致的编码,不会出问题
解决途径二:
使用new XMLWriter(new FileOutputStream(...), outputFormat)的构造方法
OutputFormat xmlFormat = OutputFormat.createPrettyPrint(); xmlFormat.setEncoding("utf-8"); XmLWriter writer = new XMLWriter(new FileOutputStream(...), xmlFormat); writer.write(document); writer.close();
如上,setEncoding可以设置存储的文件编码格式,createPrettyPrint是得到美化xml格式输出。这样的话,在不同的环境下可以获得同样的编码读写,并且真正保证了文件标称与实际编码的一致性。
注意如果使用OutputFormat是为了设置文件编码,那千万别用 XMLWriter(new FileWriter(...), outputFormat)构造方法,因为如前面所说,FileWriter不会处理编码,
所以即使你使用format.setEncoding("utf-8");他仍然不会使用utf-8编码,而只是把文件头指定为utf-8,这类似不使用outputFormat的情况。
以下为个人实践代码:
/*** 输出xml文件* * @param document* @param filePath* @throws IOException*/public static void writeXml(Document document, String filePath) throws IOException {File xmlFile = new File(filePath);XMLWriter writer = null;try {if (xmlFile.exists())xmlFile.delete();writer = new XMLWriter(new FileOutputStream(xmlFile), OutputFormat.createPrettyPrint());writer.write(document);writer.close();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (writer != null)writer.close();}}
@Testpublic void testXMLDoc() {try {String filePath = "E:/eXML.xml";Document document = XMLUtil.getDocument(filePath);Element root = null;document = XMLUtil.createDocument("vrvscript", "Class", "POLICY_BASE_LINE");root = document.getRootElement();root.addAttribute("P_ID", "12");root.addAttribute("StartPolicy", "1中文");root.addAttribute("PolicyVersion", "1.0");root.addAttribute("ScheduleMode", "6");root.addAttribute("ScheduleTime", "1:1:1");root.addAttribute("RuleHandle", "2");XMLUtil.writeXml(document, filePath);} catch (IOException e) {e.printStackTrace();}}
测试结果发现:当写入的内容包含中文时产生的xml文件是UTF-8;但如果写入的内容中不包含中文,仅包含ANSI字符,那么产生的xml文件就是ANSI
@Testpublic void testXMLDoc() {try {String filePath = "E:/eXML.xml";Document document = XMLUtil.getDocument(filePath);Element root = null;document = XMLUtil.createDocument("vrvscript", "Class", "POLICY_BASE_LINE");root = document.getRootElement();root.addAttribute("P_ID", "12");root.addAttribute("StartPolicy", "1");root.addAttribute("PolicyVersion", "1.0");root.addAttribute("ScheduleMode", "6");root.addAttribute("ScheduleTime", "1:1:1");root.addAttribute("RuleHandle", "2");XMLUtil.writeXml(document, filePath);} catch (IOException e) {e.printStackTrace();}}
转载于:https://www.cnblogs.com/liaojie970/p/5280074.html
xmlWriter 以UTF-8格式写xml问题相关推荐
- xmlWriter以UTF-8格式写xml问题
转载自:https://www.cnblogs.com/liaojie970/p/5280074.html dom4j中的XMLWriter提供以下几种构造方法: XMLWriter() XMLWri ...
- 使用XmlWriter写XML文件
private const string filename = "demo.xml";static void Main(string[] args){XmlTextWriter w ...
- JAVA写XML乱码问题_java 写 xml 中文乱码
今天用java写xml文件,发现写完后xml文件的中文就变成了乱码, 解决办法:把xml文件的头部编码改为utf-8,StreamResult编码方式也改为utf-8. =============部分 ...
- XML系列之--解析电文格式的XML(二)
上一节介绍了XML的结构以及如何创建.讲到了XML可作为一种简单文本存储数据,把数据存储起来,以XML的方式进行传递.当接收到XML时,必不可少的就是对其进行解析,捞取有效数据,或者将第三方数据以节点 ...
- tinyxml语法讲解之写xml
TinyXml 简介 Qt+TinyXML 环境搭建 环境搭建 TinyXML 框架解析 DOM 对象模型 类图关系 常用接口 写 XML TinyXml 简介 TinyXML 是一个开源的解析 XM ...
- java dom4j 写xml文件_Java实现——Dom4j读写XML文件
1. dom4j概述 解析DOM4J是一个开源XML解析包,采用了Java集合框架并完全支持DOM,SAX和JAXP. 最大的特色是使用了大量的接口,主要接口都在org.dom4j里定义. 2. do ...
- 将两个不同格式的XML文件,进行节点对照,并生成一个用于对照功能的XML
经常有的需求是,需要将一种格式的XML转换成另一种XML.如果要实现这个功能首先需要将两个不同XML手动建立节点对照关系.然后存成XML或者数据文件. using System; using Syst ...
- labelimg标注的VOC格式标签xml文件和yolo格式标签txt文件相互转换
目录 1 labelimg标注VOC格式和yolo格式介绍 1.1 voc格式 1.2 yolo数据格式介绍 2 voc格式数据和yolo格式数据相互转换 2.1 voc转yolo代码 2.2 yol ...
- 服务器端与客户端之间进行数据传输与交换的格式(xml与JSON)
服务器端与客户端之间进行数据传输与交换的格式(xml与JSON) ① 什么是数据交换格式 数据交换格式,就是服务器端与客户端之间进行数据传输与交换的格式. 前端领域,经常提及的两种数据交换格式分别是 ...
最新文章
- QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connect
- tf.name_scope()和tf.variable_scope()
- oracle 10g学习之分组函数
- SQL Server 输出 XML
- conda 升级_jupyter notebook升级体验!笔记本神器—Jupyter Lab
- win7为什么打开桌面上的计算机很卡很慢,如何解决win7系统电脑反应慢
- Android学习资料网站搜集
- 【ERP流程图】:生产制造
- PMP课程笔记:第8章 项目质量管理
- 博科FC光纤交换机详细配置教程
- 谎言,统计数据和供应商
- 一个神奇的大学科目《软件工程》,知识点总结+测试题,包你不挂科
- centos7 polkitd[661]: Unregistered Authentication Agent for unix-process:1781:115097 (system bus nam
- echarts柱形图X轴Y轴相关配置
- lay和lied_lie-lie-lay三个动词的区别
- 动态标题文字PR模板 300+高质量MOGRT字幕PR动态图形模板包
- 云图-CADViewerX 7.X OCX
- 好像还挺好玩的GAN重制版4——Pytorch搭建SRGAN平台进行图片超分辨率提升
- Android中的消息推送
- ipad分屏功能怎么开启_英雄联盟手游设置怎么调最合适?英雄联盟手游设置方法与新手开启功能解析...