Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read xml in UTF-8 in scala

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?

like image 727
oshai Avatar asked Sep 25 '11 13:09

oshai


2 Answers

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")) 
like image 143
Arjan Blokzijl Avatar answered Nov 11 '22 15:11

Arjan Blokzijl


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

like image 21
Adam Rabung Avatar answered Nov 11 '22 13:11

Adam Rabung