read xml in UTF-8 in scala

2020-08-17 17:53发布

问题:

I am trying to read a file to xml with the following code:

import scala.xml._

object HebrewToEnglishCityTranslator {

  val data = XML.loadFile("cities_hebrew_utf.xml");

  for(val entry <- data \\ "city") {
    val hebrewName = (entry \\ "hebrew_name").text
    val englishName = (entry \\ "english_name").text
    println(hebrewName + "=" + englishName)   }

However, my file is encoded in UTF-8 (hebrew chars) and XML encoding is val encoding = "ISO-8859-1"

what should I do?

回答1:

You should use XML.load(reader: java.io.Reader), which allows you to specify the file encoding:


XML.load(new java.io.InputStreamReader(new java.io.FileInputStream("cities_hebrew_utf.xml"), "UTF-8")) 


回答2:

Use the InputStream constructor instead of the String constructor. Good explanation of Stream vs. Reader xml reading here: Producing valid XML with Java and UTF-8 encoding