如何使用Scala将字符串映射作为属性包含在xml标记中?

时间:2011-02-03 16:40:21

标签: xml scala

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.?} />;
}

我正在尝试生产

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

如果可能,我想使用xml文字,但是如果不是我需要使用哪些xml类?

1 个答案:

答案 0 :(得分: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)
  }
}