Damian
Damian

Reputation: 5561

Scala XML: create a node not using literals

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>?

Upvotes: 13

Views: 4008

Answers (2)

Daniel C. Sobral
Daniel C. Sobral

Reputation: 297155

On Scala 2.8:

<xml>{content}</xml>.copy(label = tag)

Upvotes: 24

retronym
retronym

Reputation: 55028

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>

Upvotes: 11

Related Questions