Skip to content

Commit c23ffd6

Browse files
authored
Merge pull request #122 from Rogach/master
Change default encoding in XML.save to UTF-8
2 parents ddc4bb0 + c416c4e commit c23ffd6

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

jvm/src/test/scala/scala/xml/XMLTest.scala

+23
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,29 @@ class XMLTestJVM {
300300
</wsdl:definitions>""", wsdlTemplate4("service4", () => "target4") toString)
301301
}
302302

303+
// Issue found with ISO-8859-1 in #121 that was fixed with UTF-8 default
304+
@UnitTest
305+
def writeReadNoDeclarationDefaultEncoding: Unit = {
306+
val chars = ((32 to 126) ++ (160 to 255)).map(_.toChar)
307+
val xml = <x>{ chars.mkString }</x>
308+
309+
// com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException:
310+
// Invalid byte 1 of 1-byte UTF-8 sequence.
311+
// scala.xml.XML.save("foo.xml", xml)
312+
// scala.xml.XML.loadFile("foo.xml").toString)
313+
314+
val outputStream = new java.io.ByteArrayOutputStream
315+
val streamWriter = new java.io.OutputStreamWriter(outputStream, XML.encoding)
316+
317+
XML.write(streamWriter, xml, XML.encoding, false, null)
318+
streamWriter.flush
319+
320+
val inputStream = new java.io.ByteArrayInputStream(outputStream.toByteArray)
321+
val streamReader = new java.io.InputStreamReader(inputStream)
322+
323+
assertEquals(xml.toString, XML.load(streamReader).toString)
324+
}
325+
303326
@UnitTest
304327
def t0663 = {
305328
val src = scala.io.Source.fromString("<?xml version='1.0' encoding='UTF-8'?><feed/>")

shared/src/main/scala/scala/xml/XML.scala

+6-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ object XML extends XMLLoader[Elem] {
6363
val preserve = "preserve"
6464
val space = "space"
6565
val lang = "lang"
66-
val encoding = "ISO-8859-1"
66+
val encoding = "UTF-8"
6767

6868
/** Returns an XMLLoader whose load* methods will use the supplied SAXParser. */
6969
def withSAXParser(p: SAXParser): XMLLoader[Elem] =
@@ -73,6 +73,10 @@ object XML extends XMLLoader[Elem] {
7373
* Saves a node to a file with given filename using given encoding
7474
* optionally with xmldecl and doctype declaration.
7575
*
76+
* Note: default encoding was ISO-8859-1 (latin1) in pre-1.0.7 scala-xml versions.
77+
* If your code depends on characters in non-ASCII latin1 range, specify
78+
* ISO-8859-1 encoding explicitly.
79+
*
7680
* @param filename the filename
7781
* @param node the xml node we want to write
7882
* @param enc encoding to use
@@ -82,7 +86,7 @@ object XML extends XMLLoader[Elem] {
8286
final def save(
8387
filename: String,
8488
node: Node,
85-
enc: String = encoding,
89+
enc: String = "UTF-8",
8690
xmlDecl: Boolean = false,
8791
doctype: dtd.DocType = null): Unit =
8892
{

0 commit comments

Comments
 (0)