How to load (open and read) an XML file in Scala

Scala FAQ: How do I load an XML file in Scala? (How do I open and read an XML file in Scala?)

I demonstrated this in my earlier Scala XML - Searching XMLNS namespaces, XPath tutorial, but you can load an XML file in Scala like this:

import scala.xml.XML
val xml = XML.loadFile("/Users/al/Projects/Scala/yahoo-weather.xml")

Once you've loaded the file contents like that, you can manipulate them as desired, such as searching the XML using XPath constructs like this:

val temp = (xml \\ "channel" \\ "item" \ "condition" \ "@temp") text

In summary, if you need to load an XML file in Scala, I hope this has been helpful.

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.