如何在不使用文字的情况下在Scala中创建节点?
我需要的是在运行时设置节点标签名称,例如:
var tag = "post"
var content = "234"
如何获得节点<post>234</post>
?
答案 0 :(得分:22)
在Scala 2.8上:
<xml>{content}</xml>.copy(label = tag)
答案 1 :(得分:11)
scala> import xml._
import xml._
scala> def textElem(name: String, text: String) = Elem(null, name, Null, TopScope, Text(text))
textElem: (name: String,text: String)scala.xml.Elem
scala> textElem("foo", "bar")
res0: scala.xml.Elem = <foo>bar</foo>