Seeing the power of Scala, I wonder if an arbitrary object graph could be serialized and deserialized to/from XML using built-in Scala language features and libraries (e.g. without XMLEncoder, XStream or JAXB). Unfortunately, I haven't found such a solution. What could you advise?
I don't know "if an arbitrary object graph could be serialized and deserialized to/from XML using built-in Scala language features and libraries," but since there are some native support for XML in Scala, I'll mention them. More detail could be found in Ch. 26 of Programming in Scala called Working with XML:
This chapter introduces Scala's support for XML. After discussing semi-structured data in general, it shows the essential functionality in Scala for manipulating XML: how to make nodes with XML literals, how to save and load XML to files, and how to take apart XML nodes using query methods and pattern matching.
To quickly summarize the chapter, I'll quote some key points.
So you can write something like:
val foo = <a> {3 + 4} </a>
The above evaluates to scala.xml.Elem = <a> 7 </a>
.
\
with the name of the tag.\\
instead of the \
operator.The book has an example of serialization and deserialization of an abstract class, but it's hand-written:
abstract class CCTherm {
val description: String
val yearMade: Int
def toXML =
<cctherm>
<description>{description}</description>
<yearMade>{yearMade}</yearMade>
</cctherm>
def fromXML(node: scala.xml.Node): CCTherm =
new CCTherm {
val description = (node \ "description").text
val yearMade = (node \ "yearMade").text.toInt
}
}
Also more info could be found in a draft book called scala.xml.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With