tags:

views:

205

answers:

1

I am processing a XML document and iterating through nodes. I want to iterate through 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.

+4  A: 

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
Thomas Jung
That would work.Is there is no "append to list" way of doing this?
Berlin Brown
@Berlin Why do you want to explicity append to a list? ‘map‘ appends to a new collection internally.
Ben Lings