2012-02-21 2 views
2

저는 스칼라와 메신저를 배우면서 XML 파일을지도로 가져 오려고합니다. XML은 <word></word><description></description>을 사용하여 정의를 보유합니다. 지도를 만들려고 할 때 오류가 발생합니다.스칼라 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> 

답변

2

당신은 불변의 데이터 구조를 변경할 수 없습니다, 즉 컴파일러는 말을하고 싶은거야.

map(word) = description 

업데이트하는 대신 지속적으로지도에 레코드를 추가해야합니다.

map + (word -> description) 
+0

완벽! 고맙습니다. 그건 의미가 있습니다 :) – meriley

관련 문제