Working With XML


Hi All,

In this blog, I will share some basic information that will help you more to 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 to Scala REPL and  Scala will automatically interpret them as XML elements.

so let’s do some basics example 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 apply  .text on foo and it gave me the text values of each XML tag without any space.

if you want to retrieve the value of the “bar” tag then, in that case, you can use “\” Selector and tag name like –

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 in res1 then you can apply map over it and can take text value of every tag like –

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 here the tag which is the “bar” has the property “type” so you can do like –

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 wants to select all node in case 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 here you want to retrieve XML tag “title” and you tried this

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

so here result is a blank NodeSeq() because in this operation it’s 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 –

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 result that has XML tag “title” basically \\ selector used for deep search

I hope it will make easy to work with XML files


knoldus-advt-sticker


This entry was posted in Scala. Bookmark the permalink.

One Response to Working With XML

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s