Oliver Mielentz
Oliver Mielentz

Reputation: 95

XML Data to List in scala

I got a problem which seems to be fairly common but i really don't find a working approach to solve it. i have a large xml document and will parse it. the document structure is as follows:

<response>
<result>
    <doc>
        <float name="xxx">0.0</float>
        <int name="yyy">123</int>
        <str name="zzz">hello</str>
        <str name="xyz">world</str>
    </doc>
</result></response>

and i would like to parse this information into a hash map like "Map[String, Int](yyy, 123)"(One Map fo revery Datatype).

All the examples that i found until now are explaining how i can get a result like "zzzhelloxyzworld" if i look for the "str" tag or how i can find out the "name" attribute inside an "str" tag but i know the name attribute and would like to have the information in the "str" tag.

(xmldoc \\ "str").map(n => (n \ "@name").text)

for example gives me a map of all the attribute values. but i don't know a way to get to the data.

i hope someone can help me out here because it seems to me as such a common task and i don't know why i couldn't find any solution to this.

Upvotes: 3

Views: 257

Answers (1)

viktortnk
viktortnk

Reputation: 2757

(xmldoc \\ "str").map(n => (n \ "@name").text -> n.text).toMap

gives

scala.collection.immutable.Map[String,String] = Map(zzz -> hello, xyz -> world)

Upvotes: 1

Related Questions