I must be doing some stupid mistake. I have a server that returns the XML <a><b>123</b></a> and now I would like to match against that XML. So I write something like
xml match {
case <a><b>{_}</b></a> => true
}
This works as long as I do not have to deal with multi-line XML literals. So the important thing is that the server sends me the whole XML as a one-liner. The XML is large enough to explode a single line of code, but I can not figure out how to get this to work.
Server sends <a><b>123</b><c>123</c><d>123</d><e>123</e><f>123</f></a> and I would like to do this:
xml match {
case <a>
<b>{_}</b>
<c>{valueOfC}</c>
<d>{_}</d>
<e>{_}</e>
<f>{_}</f>
</a> => valueOfC
}
But I always get a MatchError. If I write everything in a single line it works. So the question is: how can I match XML while writing human-readable code?
I have of course tried to find an answer via Google. Funny enough all examples are one-liners or work recursive.
This is considerably uglier than I had initially imagined. I do have a partial solution, but I’m not sure it’s worth the effort. The default pattern match treats whitespace as tokens, and I’ve not found any clean way to get around it. So I’ve done the opposite: decorate the input string with whitespace. This example has just a single level of indentation; you could imagine recursing the whitespace-addition to match your favorite indentation style.
Here’s the example (need to compile and run; the 2.7 REPL at least doesn’t seem to like multi-line XML in case statements).
Rather inelegant, but it does (marginally) achieve what you want.