Core_Dumped
Core_Dumped

Reputation: 4719

Scala: How do I programmatically create an XML tag and insert a value in it?

I currently have something like this:

val green = "GREEN"

I want to programmatically create a tag and insert green as its only child. it should look like this:

val blue = if(blah) "INDIGO" else "SKY"
val fooElem:Elem = <{blue}> green<{blue}>

obviously, this does not work. I then find out that an empty Elem can be generated like this:

val fooEmpty = new Elem(null, blue, scala.xml.Null , scala.xml.TopScope, false) //this is <INDIGO></INDIGO> or <SKY></SKY>

Now, I do not know how to programmatically add green as its child/value so that it becomes fooElem. How can I do this?

Upvotes: 0

Views: 540

Answers (1)

Makis Arvanitis
Makis Arvanitis

Reputation: 1195

like that:

val fooEmpty = new Elem(null, blue, scala.xml.Null , scala.xml.TopScope, false, Text(green))

The constructor is:

new
Elem(prefix: String, label: String, attributes1: MetaData, scope: NamespaceBinding, minimizeEmpty: Boolean, child: Node*)

so you can pass the child nodes as the last argument

Upvotes: 1

Related Questions