views:

648

answers:

2

Hi,

Even if the question subject seems complicated, the issue is quite simple.

I create an XML file with the following script:

def xmlFile = new File("file-${System.currentTimeMillis()}.xml")
mb = new groovy.xml.StreamingMarkupBuilder()
mb.encoding = "UTF-8"
new FileWriter(exportXmlFile) << mb.bind {
    mkp.xmlDeclaration()
    out << "\n"
    someMarkup {}
}

Then when I parse this file using code like:

def xml = new XmlSlurper().parse(xmlFile)

I got the following MalformedByteSequenceException exception:

Exception thrown: Invalid byte 2 of 3-byte UTF-8 sequence

And if I convert the file in UTF-8 format (using Notepad++ for instance) then everything is ok.

So, what can I do to save my file in UTF-8 format? Why the code mb.encoding = "UTF-8" does not do it?

Thx

A: 

You need to wrap an output stream writer around a FileOutputStream is utf-8 is not the default charset

new OutputStreamWriter(new FileOutputStream(exportXmlFile),'utf-8') << mb.bind {
    mkp.xmlDeclaration()
    out << "\n"
    someMarkup {}
}

I'm not sure what setting mb.encoding does, probably just sets the charset in the xml header

leebutts
Thx. It works well.
fabien7474
A: 

You can refer to this blog. http://mrhaki.blogspot.com/2009/10/groovy-goodness-creating-xml-with.html it should help

Amit Jain