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?
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"))
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With