3

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.

1 Answer 1

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

gives

scala.collection.immutable.Map[String,String] = Map(zzz -> hello, xyz -> world)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.