Sunday, August 8, 2010

WSDL Generation with Groovy

I am fed up of crafting WSDL by hand, it's mostly fluff and it can be generated from a template.

I don't like template languages like Velocity either.

So I have decided to generate my WSDL in Grovvy.

I have installed the Eclipse Plugin:


Here a good example of how to parse XML with Groovy:

The XML defining the service is:



First create a Groovy Project, then a Groovy class - this is only a POC on how to parse XML:

class WSDLGenerator {

 static main(args) {
  File file = new File("resources/geowsdl.xml");
  def records = new XmlParser().parseText(file.getText());
  def servicename = records.servicename.text();
  def version = records.version.text();
  def schemalocation = records.schemalocation.text();
  def operations = records.operations;
  for (operation in operations.operation) {
   println operation.text();

It's as simple as that! In Java it would have taken me 5 times more code!

I will never edit manually a WSDL again, all you need is a XSD with your domain model, and you can generate the blessed WSDL by script.

No comments: