Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 550127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:15:21+00:00 2026-05-13T11:15:21+00:00

I must be doing some stupid mistake. I have a server that returns the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:15:21+00:00Added an answer on May 13, 2026 at 11:15 am

    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).

    object Test {
    
    import scala.xml._
    
    def whiten(xml: Node,w:String): Node = {
      val bits = Node.unapplySeq(xml)
      val white = new Text(w)
      val ab = new scala.collection.mutable.ArrayBuffer[Node]()
      ab += white;
      bits.get._3.foreach {b => ab += b ; ab += white }
      new Elem(
        xml.prefix,
        xml.label,
        xml.attributes,
        xml.scope,
        ab: _*
      );
    }
    
    val xml = <a><b>123</b><c>Works</c></a>
    
    def main(args:Array[String]) {
      whiten(xml,"""
             """  // You must match the multiline whitespace to your case indentation!
      ) match { 
        case <a>
             <b>123</b>
             <c>{x}</c>
             </a> => println(x)
        case _ => println("Fails")
      }
    }
    
    }
    

    Rather inelegant, but it does (marginally) achieve what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I must be doing something stupid here, because I have some very similar code
I have found out that I am doing WPF wrong and frustratingly must overhaul
I feel like I must be doing something really stupid. What can I do
This must be something utterly stupid that I've done or am doing, but I
I have this problem where all URLs like context/path/file/anything/that/follows becomes context/path/file.php/anything/that/follows or context/path/file.xml/anything/that/follows .
I must be doing something stupid, because this seems like such a easy operation.
I am doing some sample programs to explore C and would like to know
I think I must be doing something stupid. Unfortunately, due to the nature of
This is a very simple issue. I must just be doing something stupid: This
I'm doing some ASP.NET development in VS and have just found an interesting little

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.