Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create a Map from scala.xml.NodeSeq

I have the following xml-node:

val xml = <fields><field name="one"></field><field name="two"></field></fields>

Now I would like to create a Map[String, Node] with the field-name as key.

for{x <- xml \ "field"} yield Map(x \ "@name" -> x)

Using yield above I get a List of Maps though:

List(Map((one,<field name="one"></field>)), Map((two,<field name="two"></field>))) 

How do I functionally get a Map[String, Node] without going the imperative way (temp-vars) to transform the Maps in the List to the final desired Map, maybe without yield?

like image 549
trajectory Avatar asked Nov 27 '25 20:11

trajectory


1 Answers

  xml \ "field" map { x => ((x \ "@name").text -> x) } toMap
like image 102
The Archetypal Paul Avatar answered Nov 30 '25 21:11

The Archetypal Paul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!