Chinaunix首页 | 论坛 | 博客
  • 博客访问: 89237
  • 博文数量: 29
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 248
  • 用 户 组: 普通用户
  • 注册时间: 2014-01-02 18:03
文章分类
文章存档

2014年(29)

我的朋友

分类: Java

2014-03-06 17:07:52

写这篇博文的原因在于昨晚我在写服务器处理的过程中竟然花了三个小时在怎么用XMLWriter、OutputFormat类把一个Document对象以UTF-8的编码形式写入文件当中...

先看这段代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
    
this.createFile(fileURL);//如果文件不存在则建立文件
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
            SAXReader reader = new SAXReader();//实例化SAXReader
            try {
                Document document = reader.read(new File(fileURL));//读取文件
                Element root = document.getRootElement();
                Element messages = root.element("messages");
                Element message = messages.addElement("message");
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                message.addElement("from").setText("[系统公告]");
                message.addElement("to").setText("");
                message.addElement("content").addCDATA(""+
                userName+
                "走进了聊天室!
");
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                message.addElement("sendTime").setText(new Date().toLocaleString());
                OutputFormat format = new OutputFormat();//建立格式化对象
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                XMLWriter writer = new XMLWriter(new FileWriter(fileURL),format);//写入文件当中
                writer.write(document);
                writer.flush();//清除缓存
                writer.close();//关闭输出流
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
            } catch (Exception e) {
                e.printStackTrace();
            }

这里看不出什么错误,假设第一次执行这段代码,Myeclipse中并没有抛出异常。但是我们在硬盘中找到相关修改过的xml文件,然后用浏览器打开,会发现:

wKiom1MYAsah_LUYAADS8awlYj4292.jpg

如果这时候再次运行,将会抛出异常。


这是为什么呢?


是页面没有设置编码为"UTF-8"吗?


恐怕不是的,我在写这个项目的时候会先写一个filter把所有请求的编码改成UTF-8,在处理请求的Servlet的时候也会把request,response的encoding设置成UTF-8。


是因为在写入的时候,相关的对象没有处理吗?

我查了一下相关文档然后加入三行代码。
1
2
3
    
reader.setEncoding("UTF-8");//SAXReader的设置编码方法
document.setXMLEncoding("UTF-8");//document对象的设置XML文件的方法
format.setEncoding("UTF-8");//格式输出对象的设置编码方法

根本一点作用都没有!


因为博主固执地想用UTF-8格式的编码把XML对象写入文件当中,开始从各种各样的论坛寻找答案,也尝试了很多不同的方法,结果均以失败告终。

最后怎么解决问题的呢?

只需要加入一行代码:
1
    
format.setEncoding("GBK");

没错,就是把UTF-8改成GBK!


我从这次调BUG的过程中得到的最大感受就是,搞开发,不能太固执,要学会灵活应变。从长远来看,这不正是互联网所需要的精神吗?当然这会锻炼你一个人解决问题的能力,能独自解决一个问题,是一个IT人的必备素养。


最后,这是我本人的意见以及感受,如果本文中有什么错误或者纰漏,还望斧正。大家如果能有什么更好的方法,例如能有方法实现UTF-8编码XML文件写入硬盘,希望能和我交流交流,博主虚心学习,因为能进步才是最好的。
阅读(1457) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~