2

所以我有

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

我想写类似的东西

val result = <span> {list.map(toElement).toElem} <span>
4

2 回答 2

1

如果我对您的理解正确,我认为您所追求的可能是这样的:

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

但我想这实际上取决于您期望列表中的对象类型,以及您希望它们最终看起来像什么类型的 XML 元素。

于 2012-03-15T00:44:46.593 回答
0

只是一个想法...

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>

结果是

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

于 2012-03-15T00:45:42.397 回答