2011-02-03 17 views
0
val name = "Name"; 
val value = "Value"; 
val map = Map(id -> "5", class -> "Nice"); 
textfield(name, value, map); 


def textfield(name: String, value: String, attributes: Map = {}){ //=> Any 
    val xml = <text name={name} value={value} {attributes.?} />; 
} 

我想有一個生產如何在使用Scala的xml標記中包含字符串映射作爲屬性?

<text name="Name" value="Value" id="5", class="nice" /> 

我想如果可以使用XML文本,但如果沒有哪個XML類,我需要使用?

回答

2
import scala.xml.{Attribute, Text, Elem} 
def textfield(name: String, value: String, attributes: Map[String, String] = Map()) = { 
    val elem: Elem = <text name={name} value={value} /> 
    (elem /: attributes) { 
    case (el, att) => el % Attribute(att._1, Text(att._2), xml.Null) 
    } 
} 
相關問題