Scala reading content from the Web

Written on October 16th, 2009 by Thorno shouts

I use scala.io.Source in my previous post, and now I am going to show another usage of this util. Let’s write a simple RSS feed parser that reads feed from my website and parse it.

Here’s the code of it.

package com.justthor.reader

import scala.io.Source
import scala.xml._

object SimpleFeedReader {
  def main(args : Array[String]) : Unit = {
    val url = "http://just-thor.com/feed/"
    val data = XML.loadString( Source.fromURL(url).mkString )
    data \ "channel" \ "item" foreach(
      a => println((a \ "title" text) + " == "  + (a \ "link" text))
    )
  }
}

Ok,let’s go thru the code line by line.

At line 09, again we use Source to load content from url,by calling the method fromURL(quite obvious,right?).The mkString will create a String out of the content. Then, we use XML.loadString to parse the String into an XML object.

At line 10, we traverse the XML object with something like XQuery.
The RSS xml format is something like this

<channel>
<link>...</link>
<description>...</description>
<!-- item 1-->
<item>
  <title></title>
  <link></link>
</item>
<!-- item 2-->
<item>
  <title></title>
  <link></link>
</item>
<!-- and the rest goes here -->
</channel>

So when we write

data \ "channel" \ "item"
, we mean traverse to channel node, then item node of it.It will return a collection of item nodes, and then we loop it. At line 11, we will print the text within the title node and the link node.

Here’s the output of the result

File Reading in Java and Scala == http://just-thor.com/2009/10/01/file-reading-in-java-and-scala/
PHP communicate with Java using JSON – Unicode Version == http://just-thor.com/2009/09/28/php-communicate-with-java-using-json-unicode-version/
Scala And JDBC == http://just-thor.com/2009/09/27/scala-and-jdbc/
A mistake or fate? == http://just-thor.com/2009/09/17/a-mistake-or-fate/
Women are weird == http://just-thor.com/2009/09/13/women-are-weird/
20 must visit sites for Java developer == http://just-thor.com/2009/09/12/20-must-visit-sites-for-java-developer/
Why do I practice pair programming? == http://just-thor.com/2009/09/08/why-do-i-practice-pair-programming/
Installing Scala on Windows == http://just-thor.com/2009/09/02/installing-scala-on-window/
Logging for different requested URI in Apache HTTP Server == http://just-thor.com/2009/09/01/logging-for-different-requested-uri-in-apache-http-server/
Say hello to Java and Scala == http://just-thor.com/2009/08/30/say-hello-to-java-and-scala/

And now we have a very simple yet functional RSS parser.

Happy coding, :)

Filed under Programming Tags:,

Leave a Reply

(required)

(required)