0

Our application receives an XML message from another system. The XML is structured like this:

<params>
  <param name="FOO" value="BAR"/>
  ...
</params>

What is the best way, using Scala's native XML processing, to return the value BAR for the parameter which is FOO, so that:

val foo = "BAR"

Thanks

1 Answer 1

1

I assume your xml is invalid with missing param closing tag, it should be, for example

var x = <params>
  <param name="FOO" value="BAR" />
  <param name="FOO2" value="BAR2" />
</params>

If you want to extract the only param FOO, I don't think you will find anything much better than

(x \ "param" find (n => (n \ "@name").toString == "FOO")).get \ "@value"

If you want to get all params, you can iterate over them:

x \ "param" foreach {n => println(n \ "@name" + " -> " + n \ "@value")}
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.