Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - Simplified Notation for XML



Groovy provides MarkupBuilder which uses a simplified notation or DSL, Domain Specific Language to create an XML structure intuitively. Using MarkupBuilder, we can define a XML structure in a concise manner using a cleaner, readable format.

Key Features of Simplified Notation Using MarkupBuilder

  • Closure based Structure − In Groovy, we represent a XML element by a nested closure and we can represent complete XML hiearchical structure using nested closures.

  • Implicit Element Creation − Name of closure automatically becomes the name of the XML element.

  • Define Attribute − Attributes are defined as named arguments within a closure.

  • Text Element − Last string argument, or last expression of a closure is considered as text content of XML element.

Creating a Simple XML using Simplified Notation

For example, if we need to create following XML

<employee age="30">
   <name>Alice</name>
   <department>Admin</department>
</employee>

Now using MarkupBuilder, we can create above XML with Simplified Notation as shown below −

Example.groovy

import groovy.xml.MarkupBuilder

def writer = new StringWriter()
def xml = new MarkupBuilder(writer)

xml.employee(age: 30) {
   name 'Alice'
   department 'Admin'
}

def generatedXml = writer.toString()
println generatedXml

Output

When we run the above program, we will get the following result.

<employee age='30'>
  <name>Alice</name>
  <department>Admin</department>
</employee>

Explanation

  • xml.employee(age: 30) {...} − Creates a employee element where age: 30 defines the attribute age with value 30. The closure {...} represents the child elements of employee node.

  • name 'Alice' − Creates a <name> element with text content Alice.

  • department 'Admin' − Creates a <department> element with text content Admin.

Creating a Complex XML using Simplified Notation

For example, if we need to create XML to represent a list of employess.

<employees>
   <employee age="30">
      <name>Alice</name>
      <department>Admin</department>
      <salary>10000<salary>
   </employee>
   <employee age="35">
      <name>Adam</name>
      <department>Finance</department>
      <salary>11000<salary>
   </employee>
   <employee age="32">
      <name>Bob</name>
      <department>Finance</department>
      <salary>11000<salary>
   </employee>
</employees>

Now using MarkupBuilder, we can create above XML with Simplified Notation as shown below −

Example.groovy

import groovy.xml.MarkupBuilder

def writer = new StringWriter()
def xml = new MarkupBuilder(writer)

xml.employees{
   employee(age: 30) {
      name 'Alice'
      department 'Admin'
	  salary 10000
   }
   employee(age: 35) {
      name 'Adam'
      department 'Finance'
	  salary 11000
   }
   employee(age: 32) {
      name 'Bob'
      department 'Finance'
	  salary 11000
   }
}

def generatedXml = writer.toString()
println generatedXml

Output

When we run the above program, we will get the following result.

<employees>
  <employee age='30'>
    <name>Alice</name>
    <department>Admin</department>
    <salary>10000</salary>
  </employee>
  <employee age='35'>
    <name>Adam</name>
    <department>Finance</department>
    <salary>11000</salary>
  </employee>
  <employee age='32'>
    <name>Bob</name>
    <department>Finance</department>
    <salary>11000</salary>
  </employee>
</employees>

Advantages of Simplified Notation

  • Improved Readability − Groovy code using MarkupBuilder closely resembles the XML structure which makes code highly understandable and maintainable.

  • Concise Code − Simplified Notation is much less verbose than traditional XML APIs.

  • Automatic Type Handling − Groovy handles type conversion in most of the cases automatically while setting attribute/element content.

  • Nesting − Nested Closures represents hierachical XML struture seemlessly.

Advertisements