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!
-
h2("Vær for " + weather.location.name)
-
p {
-
(w.symbol.@number.toInteger()<9 && w.@period == "0"?"n":"") + ".png")
-
span("class":w.temperature.@value.toInteger()<0?"minus":"plus" + " temperature",
-
w.temperature.@value.text() + "°")
-
br()
-
span("class":"wind", w.windSpeed.@name.text() + ", " +
-
w.windSpeed.@mps.text() + " m/s fra " + w.windDirection.@name.text())
-
}
-
p("class":"nag", "Varsel fra ") {
-
a("href":yrUrl,"yr.no")
-
}
-
}
-
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..
skikkelig boring! dette gadd jeg ikke lese engang
Hva! Trodde jo du syntes dette var kjempespennende??
Ikke relatert til denne bloggposten, men bare lurte på om dere har starta med skobissniss? http://www.eshoes.dk/shop/rubber-duck-54c1.html?gclid=CMeZ9qfuk5cCFUoa3godXn93JA
oprindelig tænkt som en vinterstøvle.
Men pga. det stærke modelook bruges den nu næsten hele året rundt.