In this blog, we will talk about how we can work with XML using Scala.
Scala treats XML as the first-class citizen. So, instead of embedding XML documents into strings. , you can place them inline in your code like you place an int or double value.
scala> val xml = <greet>Hello</greet>
xml: scala.xml.Elem = <greet>Hello</greet>
res2: Class[_ <: scala.xml.Elem] = class scala.xml.Elem
We have created a val named xml and assigned xml sample content to it. Scala parses it and creates an instance of scala.xml.Elem.
The scala package scala.xml provides classes to create XML document, parse them, read them and store them.
Let’s see how we can parse it. XPath is a powerful tool to query the XML document. Scala provides an XPath like query ability with a slight difference. In XPath, we use forward slashes “/” and “//” to query the XML documents. But in Scala, “/” is a division operator and “//” is one of the way to comment the code. Scala prefers to use backward slashes “\” and “\\” to query the XML document.
scala> val xmlDoc = <symbols>
<symbol ticker="Cisco" ><units>100</units></symbol>
<symbol ticker="Sandisk" ><units>315</units></symbol>
We would like to get the symbol elements. We can use the XPath query,
scala> val children = xmlDoc \ "symbol"
scala> children: scala.xml.NodeSeq = NodeSeq(<symbol ticker="Cisco"><units>100</units></symbol>, <symbol ticker="Sandisk"><units>315</units></symbol>)
We called the \() on the XML element and asked it to look for all symbol elements. It returns an instance of scala.xml.NodeSeq, which represents a collection of XML nodes.
The \() method looks only for the elements that are direct descendants of the target element(i.e symbol). If we want to search through all the elements in the hierarchy starting from the target element, \\() method is used
val grandChildren = xmlDoc \\ "units"
grandChildren: scala.xml.NodeSeq = NodeSeq(<units>100</units>, <units>315</units>)