Updated March 30, 2023
Introduction to Scala XML
Scala XML is a way of working with the eXtensible Markup Language with the Scala Language. Scala treats XML as a first-class citizen, so we directly work by placing them in our code. XML parses the XML provided and creates an instance of it that is used further for reading purposes. Scala.XML provides the method for creating, parsing, and reading the XML file in Scala. So XML generally provides us with the package that contains methods to work with the XML file.
Syntax:
Since Scala treats the XML file as the first-class citizen only so the syntax for this is the same starting from defining the XML and then parsing it as needed.
Defining a variable in the form of XML stores that as an XML literal in Scala. Just need to import the package which can parse the XML File with it.
val a = <start>Hello</start>
a: scala.xml.Elem = <start>Hello</start>
a.getClass
res0: Class[_ <: scala.xml.Elem] = class scala.xml.Elem
So in this way, we can parse the XML and Work with the data Inside.
Working of Scala XML with Examples
The XML’s are parsed first by the compiler, the package has a tool to read the XML documents called as Xpath. Scala prefers the use of backslash “\” to query the documents in the XML. XML has two main components with it the Text and Tag, we need to specify a start and end tag while working with the XMLs.
The Xpath in the compiler uses the forward slashes to “/” “//” to read the XML docs but as these signs have a different value it prefers the backslash for querying it.
Let us check that with an Example how scala parses this XML:-
val namelist = <names>
<name> Arpit </name>
<name> Anand </name>
<name> Ram </name>
</names>
namelist: scala.xml.Elem =
<names>
<name> Arpit </name>
<name> Anand </name>
<name> Ram </name>
</names>
Here the XML is made with the / forward slash and now we will fetch the names so it uses the \ backslash for this.
val fetch = (namelist \ "name")
fetch: scala.xml.NodeSeq = NodeSeq(<name> Arpit </name>, <name> Anand </name>, <name> Ram </name>)
This fetches as all the values that are in the name tag, and to take the value elements we will use that .text method.
fetch.foreach(fetch =>println(fetch.text))
Arpit
Anand
Ram
So from this, we can fetch the elements in an XML.
We can also use the .child method to get all the children of an XML and then can take the values using the .text method. Let us check that with the help of an Example:-
In the above example, only just the namelist.child will give the list of all the children in an ArrayBuffer and we can use it accordingly.
namelist.child
res1: Seq[scala.xml.Node] =
ArrayBuffer(
, <name> Arpit </name>,
, <name> Anand </name>,
, <name> Ram </name>,
)
res1.foreach(res1 =>println(res1.text))
Arpit
Anand
Ram
Output:
Even we can use a nested XML and can traverse the elements in that XML through scala. Even the attributes tags can also be fetched from an XML using the @ symbol after that. The attributes tags are just like the key Value pair with = denoted over that.
Let us check that with an example:-
val Details =
<Name>
<Add>
<title>This is the add detail</title>
<item>
<title>Here is the add</title>
<add hno="5" lane="xyz" city="abc" country="India" code="29" />
</item>
</Add>
</Name>
Details: scala.xml.Elem =
<Name>
<Add>
<title>This is the add detail</title>
<item>
<title>Here is the add</title>
<add hno="5" lane="xyz" city="abc" country="India" code="29"/>
</item>
</Add>
</Name>
Here we created an XML with the proper body and attributes it. To traverse the elements inside the parameter we just navigate the directory wise.
val add = Details \ "Add" \ "item" \ "add"
add: scala.xml.NodeSeq = NodeSeq(<add hno="5" lane="xyz" city="abc" country="India" code="29"/>)
Using @ access the elements of an attribute of an XML
val add = Details \ "Add" \ "item" \ "add" \ "@hno"
add: scala.xml.NodeSeq = 5
val add = Details \ "Add" \ "item" \ "add" \ "@lane"
add: scala.xml.NodeSeq = xyz
val add = Details \ "Add" \ "item" \ "add" \ "@country"
add: scala.xml.NodeSeq = India
val add = Details \ "Add" \ "item" \ "add" \ "@code"
add: scala.xml.NodeSeq = 29
val add = Details \ "Add" \ "item" \ "add" \ "@country"
add: scala.xml.NodeSeq = India
val add = Details \ "Add" \ "item" \ "add" \ "@code"
add: scala.xml.NodeSeq = 29
The result what we get is of the type NodeSeq to convert it to the string we will use the .text function as stated below:-
val add = (Details \ "Add" \ "item" \ "add" \ "@code").text
add: String = 29
Also the use of \\ will directly let the attribute part to be fetched.
val add = (Details \\ "@code").text
add: String = 29
Even if it is not able to find the attribute it gives up the null as the return value.
val add = (Details \\ "@add").text
add: String = ""
val add = (Details \\ "@lane").text
add: String = xyz
Output:
So here we saw the use of various methods through which we can parse an XML.
Conclusion
From the above article, we saw how XML is used for parsing of XML data. We also saw with the various examples the methods needed to parse the XML data and certain operations over it. We also saw different examples stating the use for the same and the benefits of having it.
So we can proceed further and look over the usage and can say that XML is an important concept for handling the XML data with Scala.
Recommended Articles
This is a guide to Scala XML. Here we discuss How Scala XML is used for parsing of XML data along with the working of XML with Examples. You may also have a look at the following articles to learn more –