开发者

read xml in UTF-8 in scala

开发者 https://www.devze.com 2023-04-07 02:50 出处:网络
I am trying to read a file to xml with the following code: import scala.xml._ object HebrewToEnglishCityTranslator {

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号