【发布时间】:2010-12-04 10:57:53
【问题描述】:
即使题目看起来很复杂,其实问题也很简单。
我使用以下脚本创建了一个 XML 文件:
def xmlFile = new File("file-${System.currentTimeMillis()}.xml")
mb = new groovy.xml.StreamingMarkupBuilder()
mb.encoding = "UTF-8"
new FileWriter(xmlFile) << mb.bind {
mkp.xmlDeclaration()
out << "\n"
someMarkup {}
}
然后当我使用如下代码解析这个文件时:
def xml = new XmlSlurper().parse(xmlFile)
我得到了以下MalformedByteSequenceException 异常:
抛出异常:无效的字节 2 3 字节 UTF-8 序列
如果我将文件转换为 UTF-8 格式(例如使用 Notepad++),那么一切正常。
那么,我该怎么做才能将我的文件保存为 UTF-8 格式?为什么代码mb.encoding = "UTF-8" 不做呢?
谢谢
【问题讨论】:
-
可以参考这个博客。 mrhaki.blogspot.com/2009/10/… 应该有帮助
标签: xml grails utf-8 groovy markupbuilder