Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala XML: create a node not using literals

Tags:

xml

scala

How can I create a node in Scala without using literals?

What I need is to set the node tag name in runtime, for example:

var tag = "post"
var content = "234"

How can I get a node <post>234</post>?

like image 246
Damian Avatar asked Mar 27 '10 20:03

Damian


2 Answers

On Scala 2.8:

<xml>{content}</xml>.copy(label = tag)
like image 154
Daniel C. Sobral Avatar answered Nov 09 '22 17:11

Daniel C. Sobral


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>
like image 34
retronym Avatar answered Nov 09 '22 19:11

retronym