2012-03-15 121 views

回答

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>

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元素。

相關問題