本文介绍了在Scala中以UTF-8读取xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码将文件读取到xml:

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)   }

但是,我的文件使用UTF-8(希伯来字符)编码,XML编码是 val encoding = ISO-8859-1

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

我应该怎么做?

推荐答案

您应该使用 XML.load(阅读器:java.io.Reader),它允许您指定文件编码:

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"))

这篇关于在Scala中以UTF-8读取xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:51