Search code examples
javaxmlscalaurlconnection

Google weather api, issue reading xml


I'm using scala to read an xml from google's weather api:

val response = new URL("http://www.google.com/ig/api?weather=MyCity").openStream()
var respXML = response.read()
println(respXML)

but when i run it the println prints a number: 60 instead of the actual xml, i already tried it with the curl utility and it worked fine, forgive my ignorance but im new to java's net classes and as far as i know from the tutorials this should work.

This is all the relevant code, there isn't much more than this.

Thanks.


Solution

  • If you are using Scala, scala.io.Source is your good friend.

    import scala.io.Source
    import scala.xml.XML
    import java.net.URL
    
    val source = Source.fromURL(new URL("http://www.google.com/ig/api?weather=MyCity"))
    val xmlString = source.mkString  // Raw XML String
    val xml = XML.loadString(xmlString)  // Scala XML object