0

Is it possible to get the value of a tag based on the tags name? For example., in the following xml,

<root>
  <a>
    <b>one</b>
    <c>two</c>
  </a>
  <a>
    <b>two</b>
    <c>one</c>
  </a>
</root>

And when I do the following:

val aNodes = root \\ "a"
aNodes.map(aNode => {
  aNode. ??? // How to I get the value of b and c by using its tag name?
})

1 Answer 1

2

You can get the text content of the children elements b and c by navigating to them using the \ path projection function and calling the NodeSeq.text method on the results:

(xml \\ "a") map (e => ((e \ "b") text, (e \ "c") text)) // List((one,two), (two,one))

This returns a Tuple2 containing the values for b and c for all a elements.

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.