2

So I have

val list:List[Any];
def toElement(a:Any):scala.xml.Elem;

And I want to write somthing like

val result = <span> {list.map(toElement).toElem} <span>
yura
  • 14,489
  • 21
  • 77
  • 126

2 Answers2

1

If I've understood you correctly, I think what you're after may be something like this:

// List of different types
val list: List[Any] = List("one", 2, "three", 4:Long)

// Conversion function for type 'Any' - (note .toElem or .toXml isn't a
// member of 'Any' - so that's why we need to create this)
def toElement(a: Any): scala.xml.Elem = <hello>{ a.toString }</hello>

// Usage example
val result = <span>{ list.map( toElement(_) ) }</span>    

But I guess it really depends on what type of objects you're expecting in the list, and what sort of XML elements you want them to end up looking like.

Sandipan
  • 48
  • 5
0

Just an idea...

val list:List[Any] = List(1, 2, "test", 3.5)

def toElement(a:Any):scala.xml.Elem = {
  scala.xml.Elem(null, a.toString, scala.xml.Null, scala.xml.TopScope)
}

val result = <span> { list map toElement } </span>

results in

<span> <1></1><2></2><test></test><3.5></3.5> </span>

Kyle
  • 21,978
  • 2
  • 60
  • 61