Twistleton
Twistleton

Reputation: 2935

Scala - transform a XML literal into a string without newlines

For more clarification in a XML structure I will write every tag in a single line.

Unfortunately the result contains (after transforming to text) more than one line, so the assertion failed. I need the whole result as a single line without newlines

val row = <row>
            <fromSubsystem>02</fromSubsystem>
            <toSubsystem>01</toSubsystem>
            <action>E013</action>
            <comment>return to customer</comment>
          </row>

println("==> " + row.text)  

assert(row.text == "0201E013return to customer")   

==> 
             02
             01
             E013
             return to customer
Exception in thread "main" java.lang.AssertionError: assertion failed
    at scala.Predef$.assert(Predef.scala:146)

Thanks in advance for an elegant solution!

Pongo

Upvotes: 0

Views: 695

Answers (3)

rs_atl
rs_atl

Reputation: 8985

Change your assertion to this:

assert(row.text.split('\n').map(_.trim).mkString == "0201E013return to customer")

Upvotes: 0

Dave Griffith
Dave Griffith

Reputation: 20515

row.child.map(_.text.trim).mkString

Upvotes: 4

om-nom-nom
om-nom-nom

Reputation: 62835

Why don't you use a regexp?

assert(row.text.replaceAll("\n[ ]+","") == "0201E013") 

//or just "\n +" in replaceAll

If you don't like regexp and dont care about spaces in tag's text you can do something like this:

assert(row.text.filterNot(Set(' ','\n')) == "0201E013") 

Upvotes: 0

Related Questions