Search code examples
xmlscalascala-xml

How to convert list of scala Xml elements to single scala Elements?


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>

Solution

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