2

Now (2014) that the support for XML literals in Scala is officially deprecated, what is the recommended way to work with XML in Scala? My current requirements are XSD support, XML transformations.

One way to do these is to convert the XML input to native Scala collections, use the collections API to make the transformations, then convert back to XML (a different format). Simple, but is this really the best solution? How about XSD support?

If I had to use Java I would probably use Saxon, which I had used in the past with good experiences. Is there anything even remotely comparable for Scala?

EDIT: Trying to close this. This question should probably have been asked on programmers.stackexchange.com as it is not about a specific problem, so it is too vague for this site.

3
  • 2
    How do XSD support and XML transformations have anything to do with scala's XML literals? Care to elaborate? Commented Jun 30, 2014 at 9:53
  • This question should have been asked on programmers.stackexchange.com as it is not about a specific problem, so it is too vague for this site. Commented Jun 30, 2014 at 17:01
  • You'd use library (Saxon) in Java. Why not to use the same from Scala? Commented Jul 1, 2014 at 20:28

1 Answer 1

1

They deprecated XML literals because users were annoyed by the celebrity status it got, that is: Why should XML have higher status than alternatives like JSON?

To mend this, the Scala team added string interpolation, which allows you to do things like these:

val name = "Niko"
val age = 21

println(s"hi, I'm $name, and I'm $age years old") // prints: hi, I'm Niko, and I'm 21 years old

Note the little "s" at the beginning of the string. The scala team didn't just include the interpolated string; they also added a means for you to make your own!

So doing that you'd be able to do something like this:

val xmlObj = xml"<body>$data</body>"

or

val jsonObj = json"""{"body": "$data"}"""

And then have it convert to de-serialized xml or json objects that you can work with

You can read more about how this works in practice here: http://docs.scala-lang.org/overviews/core/string-interpolation.html

Sign up to request clarification or add additional context in comments.

1 Comment

So your suggestion is the same as mine, plus the string interpolation. Still, no schema support, no Xquery/XSLT-like high level transformations?

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.