How to load an XML URL in Scala (contents of an XML web page)

Scala XML FAQ: How do I load an XML URL in Scala? (How do I read/download the contents of an XML URL in Scala?)

To load the contents of an XML URL (web page) in Scala, such as an RSS news feed or RESTful web service, just use the load method of the Scala XML class:

val xml = XML.load("")

Here's an example of what this looks like in the Scala REPL:

scala> import scala.xml.XML
import scala.xml.XML

scala> val xml = XML.load("")
xml: scala.xml.Elem = 
<rss xml:base="" version="2.0" xmlns:dc="">

(output goes on for about ten more lines)

Once you have your XML object (an instance of the scala.xml.Elem class), you can search it as usual. Since my RSS feed includes an "item" tag, I can get the number of item tags in the XML contents:

scala> (xml \\ "item").length
res5: Int = 10

I can also do things like searching for "title" elements, such as getting the text from the first RSS title element:

scala> val firstTitle = (xml \\ "channel" \ "title").text
firstTitle: String =

As you can see, loading the contents of an XML URL in Scala is very easy.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.