开发者

Common idiom in Java to Scala, traverse/Iterate Java list into Scala list

开发者 https://www.devze.com 2022-12-28 11:08 出处:网络
I am processing a XML document and iterating through nodes.I want to iterate through开发者_如何学JAVA the nodes and build a new List of some type.How would I do this with Scala:

I am processing a XML document and iterating through nodes. I want to iterate through开发者_如何学JAVA the nodes and build a new List of some type. How would I do this with Scala:

Here is my XML traverse code:

  def findClassRef(xmlNode: Elem) = {

    xmlNode\"classDef" foreach { (entry) =>
        val name    = entry \ "@name"
        val classid = entry \ "@classId"
        println(name + "//" + classid)
    }
  }

Where the line of println is, I want to append elements to a list.


Map should work. If you do not need exactly a List instance you can remove the toList.

xmlNode \"classDef" map { (entry) =>
  val name    = entry \ "@name"
  val classid = entry \ "@classId"
  name + "//" + classid
} toList
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号