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 = Hello
xml: scala.xml.Elem = Hello
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 =
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(100, 315)
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(100, 315)
And we can use the text method to get the text node within an element. Let see another example,
scala> val document =
scala> val children = (doc \ "language")
children: scala.xml.NodeSeq = NodeSeq(Scala, Java, C++, Kotlin)
We can iterate through the NodeSeq and print the node using text.
scala> children.foreach(child => println(child.text))
There is also child method to get all the children of the root element.
scala> val children = doc.child
children: Seq[scala.xml.Node] =
How to extract the attributes from XML elements?
scala> val xmlLanguage =
scala> val attributes = xmlLanguage \\ "@platform"
attributes: scala.xml.NodeSeq = NodeSeq(jvm, jvm, clr)
scala> attributes.foreach(e => println(e))
How to load XML document from local?
XML class provides load method which takes the path as a parameter and loads XML file into memory.
val xmlFile = XML.load(path)
It will load the XML content in memory and we can easily parse it or manipulate it.
Please feel free to suggest or comment!
2 thoughts on “Working with XML in Scala2 min read”
Reblogged this on Agile Development and commented:
Working with XML in Scala
Comments are closed.