It’s about time for a new geek post!
I’ve been playing around a bit with Groovy lately, and it’s really neat, especially for processing XML stuff. It has a bunch of ways of reading it, from the more normal Java-like ways of DomBuilder to the Groovy way of XmlSlurper. And then it has it’s really cool builder syntax for constructing XML, or pretty much whatever else you can represent as a tree (Swing GUIs, JSON and so on).
Now I’d like to combine some slurping and building to show some custom formatted weather data for the city of Oslo, consumed from the Norwegian online weather portal yr.no‘s API!
That’s it. About 20 lines for reading and parsing a URL and building a HTML fragment, now let’s see you do that with plain Java and the DomBuilder… 😉
There is some added ugliness in there that probably deserves an explanation. For one, since I wanted to display temperatures below/above zero with a custom CSS class I’m checking the value of the temperature. Secondly, I wanted to use the weather images directly off NRK’s server. Symbols 1-9 starts with a leading zero and symbol 1-8 are different during nighttime..