2
所以我如何將scala Xml元素的列表轉換爲單個scala元素?
val list:List[Any];
def toElement(a:Any):scala.xml.Elem;
而且我想如果我理解你正確地寫出財產以後像
val result = <span> {list.map(toElement).toElem} <span>
所以我如何將scala Xml元素的列表轉換爲單個scala元素?
val list:List[Any];
def toElement(a:Any):scala.xml.Elem;
而且我想如果我理解你正確地寫出財產以後像
val result = <span> {list.map(toElement).toElem} <span>
只是一個想法...
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>
,我想你以後可能是這樣的:
// 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元素。