2
我剛學習scala,並試圖將XML文件導入到Map中。 XML保存使用<word></word>
和<description></description>
的定義。當我嘗試創建地圖時出現錯誤:Scala將XML導入地圖
Value update is not member of scala.collection.immutable.Map[String, String]
任何指針,將不勝感激!
package main
import scala.xml._
object main {
def main(args: Array[String]): Unit = {
val definitions = XML.load("src\\main\\Definitions.xml");
val definitionMap = (Map[String, String]() /: (definitions \ "word")) { (map , defNode) =>
val word = (defNode \ "word").text.toString()
val description = (defNode \ "description").text.toString()
map(word) = description
}
println(definitions.getClass())
println("Number of elements is " + (definitions \\ "word").size)
}
}
和XML格式爲這樣:
<?xml version="1.0"?>
<definitions>
<entry>
<word>Ontology</word>
<description>A set of representational primitives with which to model a domain of knowledge or discourse.</description>
</entry>
<entry>
<word>Diagnostic</word>
<description>The process of locating problems with software, hardware, or any combination thereof in a system, or a network of systems.</description>
</entry>
<entry>
<word>Malware</word>
<description>Software that is intended to damage or disable computers and computer systems.</description>
</entry>
<entry>
<word>Capacitor</word>
<description>A device used to store an electric charge, consisting of one or more pairs of conductors separated by an insulator.</description>
</entry>
<entry>
<word>Stress Test</word>
<description>A test measuring how a system functions when subjected to controlled amounts of stress.</description>
</entry>
<entry>
<word>Registry</word>
<description>A hierarchical database that stores configuration settings and options on Microsoft Windows operating systems.</description>
</entry>
<entry>
<word>Antivirus</word>
<description>Designed to detect and remove computer viruses.</description>
</entry>
</definitions>
完美!謝謝。這就說得通了 :) – meriley 2012-02-21 19:32:46