Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Working With XML

DZone's Guide to

Working With XML

In this post, we'll learn how to manipulate our XML files using Scala. This can be a great help when you need to make monotonous changes to your XML.

· Web Dev Zone ·
Free Resource

Bugsnag monitors application stability, so you can make data-driven decisions on whether you should be building new features, or fixing bugs. Learn more.

Hi all. In this post, I will share some basic information that will help you work with an XML file. The first thing to know about XML in Scala is that Scala can process XML literals, so you don't need to have any specific dependencies for working with XML. You can directly put the whole XML file into the Scala REPL and Scala will automatically interpret it as XML.

So let's do some basic examples to get an understanding of how to play with XML.

scala> val foo = <foo><bar type="greet">hi</bar><bar type="count">1</bar><bar type="color">yellow</bar></foo>
foo: scala.xml.Elem = <foo><bar type="greet">hi</bar><bar type="count">1</bar><bar type="color">yellow</bar></foo>

So if you want to retrieve the values of each tag then you can use:

scala> foo.text
res0: String = hi1yellow

So here you can see I just applied .text on foo and it gave me the text values of each XML tag without any spaces.

If you want to retrieve the value of the bar tag, then, in that case, you can use the \ selector and tag name like this:

scala> foo \ "bar"
res1: scala.xml.NodeSeq = NodeSeq(<bar type="greet">hi</bar>, <bar type="count">1</bar>, <bar type="color">yellow</bar>)

So here you can see the output is a NodeSeq, which is the sequence of the XML tags that has the name bar.

And if you want to access the value of each XML tag that is res1, then you can apply a map over it and can take the text value of every tag like so:

scala> (foo \ "bar").map(_.text)
res2: scala.collection.immutable.Seq[String] = List(hi, 1, yellow)

Some of you have queries about how to find out the value of tag that has the property type or how to retrieve the XML tag on the basis of property. So, in this example, you can see that the bar tag has the property "type" so you can do the following:

scala> (foo \ "bar").map(_ \ "@type")
res3: scala.collection.immutable.Seq[scala.xml.NodeSeq] = List(greet, count, color)

Note that the \ selector can only retrieve children of the node. If you want to deep search for the specific node or want to select all nodes, you have to use the \\ selector.

val weather = 
<rss>
 <channel>
 <title>Yahoo! Weather - Boulder, CO</title>
 <item>
 <title>Conditions for Boulder, CO at 2:54 pm MST</title>
 <forecast day="Thu" date="10 Nov 2011" low="37" high="58" text="Partly Cloudy" 
 code="29" />
 </item>
 </channel>
</rss>

Suppose that you want to retrieve the title XML tag and you tried this:

scala> weather \ "title"
res4: scala.xml.NodeSeq = NodeSeq()

So here the result is a blank NodeSeq() because, in this operation, it found its child XML tag which is "channel" not a "title" and did not check further child of "channel" that's why it's giving us a black NodeSeq().

But if you use \\ selector so it will be like this:

scala> weather \\ "title"
res5: scala.xml.NodeSeq = NodeSeq(<title>Yahoo! Weather - Boulder, CO</title>, <title>Conditions for Boulder, CO at 2:54 pm MST</title>)

Here you can see it's giving you all the results that have the XML tag "title." Basically, the \\ selector is used for deep searches.

I hope this post will make it easy to work with XML files.

Monitor application stability with Bugsnag to decide if your engineering team should be building new features on your roadmap or fixing bugs to stabilize your application.Try it free.

Topics:
web dev ,xml ,scala ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}