I was exchanging some mail with Rusty trying to explain why I think GroovyMarkup is useful. Here's a little example I hacked up on email (so its totally untested :) to show the kind of things its great for.
Imagine you're a developer, you need to do a database query, suck out
some data and then generate some XML that may then be spat into a web
service request/response, turned into a DOM or JAXB beans or text
markup or SAX / StAX events etc....
# lets output some XML builder # could be SAX / StAX / DOM / TrAX / text etc
xml = createXmlBuilder()
xml.customers() {
loc = 'London'
sql.queryEach ("select * from customer where location = ${loc}) {
# lets process each row by emitting some markup
xml.customer(id:it.id, type:'Customer', foo:someVariable)) {
if (it.parter == 'Y') {
role("partner")
}
name(it.customer_name)
location(id:it.location_id, name:it.location_name)
}
}
}
This could generate, dynamically something like
<customers>
<customer id="123" type="Customer" foo="whatever">
<role>partner</role>
<name>James</name>
<location id="5" name="London"/>
</customer>
...
</customers>
The thing I find really interesting about this demo is, the XML
technology used at the other end could be push-event based (SAX) or
pull-event based (StAX) or a DOM-ish API (W3C, dom4j, JDOM, EXML, XOM)
or some JAXB-ish thing (XMLBeans, Castor) or just beans or just good
old text files.
e.g. a pull parser could literally pull the data out of the database.
Or the data could be pushed into some structure or piped straight to a
file using IO or async NIO.
It means developers can hide the XML plumbing and focus on tackling the real problems we're trying to solve.
6:11:29 PM
|