Scala XML Processing - Literals, Serialization, Parsing, Save and Load Examples

XML is a form of semi structured data which is organized in the form of trees. Semi structured data is helpful when you serialize the program data for saving in a file or shipping across a network. It defines a standardized document which is easy to read an interpret. XML stands for eXtensible Markup Language.

XML consists of two basic elements text and tags. Text is a sequence of characters. Tags consists of a less than sign alphanumeric character and greater than sign. An end tag is same as start tag except that it consists of a slash in the end. Start tag and end tag must have the same label.

For example;

Above is valid XML as the start and end tag match each other.

Above is invalid XML as the end tag is not specified.

Above XML is also invalid because the standard tag which is the child should be closed first and then the parent tag school should be closed.

Since tags have to be matched, XML are structured as nested elements. The start and end tags forms a pair of matching elements and elements can be nested within each other. In the above example standard is the nested element.

The shorthand notation which is the start tag followed by the slash indicates the start and end tag. One tag with a slash indicates an empty element.

For instance in below XML standard is an empty element.

Start tags can have attributes. An attribute is a name value pair with an equal sign in the middle. The attribute is surrounded by double quotes or single quotes.

For instance

Now that we have a brief knowledge of XML, let’s look over different things we can do in Scala for XML processing.

Scala XML Literals

Type a start tag and then continue writing the XML content. The XML contents are read until the end tag is seen.

For example, Open the Scala REPL shell and execute the code as

Scala expression can be evaluated in the tag value using curly braces. For example;

Output: res1: scala.xml.Elem = <a> hi,Reena </a>

A brace escape can include arbitrary scala content including XML literals. For example;

Output: res3: scala.xml.Elem = <a> <marks> 78 </marks> </a>

The code inside the curly braces are evaluated to an XML node or a sequence of XML nodes. In the above example if the marks is less than 80 it is added to <a> element else nothing is added.

The expression inside the brace is evaluated to a scala value and then converted to string and inserted as text.

Output: res4: scala.xml.Elem = <a> 49 </a>

The <, >, and & characters in the text will be escaped if you print the node.

Output: res5: scala.xml.Elem = <a> </a>Hello Scala<a> </a>

Below image shows all the above Scala XML Literals processing in scala shell.

Serialization in Scala

Serialization converts the internal data structure to XML so that the data can be stored, transmitted or reused. Use XML literals and brace escapes to convert to XML. Use the toXML method that supports XML literals and brace escapes.

For example first of all we will define Student class and create an instance of it.

Below image shows the scala serialization process in scala shell.

Scala-XML-Serialization

Scala XML Parsing

There are many methods available for XML classes. Let us now see a very useful method as how to extract text, sub elements and attributes.

Extracting Text
The text method on the XML node retrieves the text within that node. For example;

Here the tags are excluded from the output.

Extracting sub-elements

The sub elements are extracted by calling \ followed by tag name. For example;

Below image shows the above xml parsing examples in scala shell.

Tag attributes are extracted using the same and \ methods with an at sign (@) before the attribute name. For example;

Scala De-serialization example

The XML is converted back to the internal data structure for the program to use. For example;

The Student class created during serialization process shall be used as the student class and the toXML methods are used.

Output: fromXML: (node: scala.xml.Node)Student

Now call the stud created in the serialization and print the xml content as below.

Now invoke toXML method as;

Call the fromXML method as;

Scala XML Saving into file and Loading from file

The XML.saveFull command is used to convert data to a file of bytes. The first argument is the file name to which the node is to be saved, second is the node, third is the character encoding, fourth is whether to write an XML declaration at the top that includes the character encoding and finally the fifth is the document type.

For example;

We are using the st node created above in the de-serialization process.

Now open the stud.xml file which stores the following contents:

Now for loading the file we can use the load method as;

That’s all for XML processing in Scala programming, we will look into more Scala features in coming posts.

By admin

Leave a Reply

%d bloggers like this: