
- Groovy Tutorial
- Groovy - Home
- Groovy - Overview
- Groovy - Environment
- Groovy - Basic Syntax
- Groovy - Data Types
- Groovy - Variables
- Groovy - Optionals
- Groovy - Numbers
- Groovy - Strings
- Groovy - Ranges
- Groovy - Lists
- Groovy - Maps
- Groovy - Dates & Times
Groovy Operators
- Groovy - Operators
- Groovy - Arithmetic Operators
- Groovy - Assignment Operators
- Groovy - Relational Operators
- Groovy - Logical Operators
- Groovy - Bitwise Operators
- Groovy - Spaceship Operator
- Groovy - in Operator
- Groovy - Elvis Operator
- Groovy - Safe Navigation Operator
- Groovy Operator Precedence & Associativity
Control Statements
- Groovy - Decision Making
- Groovy - If Else Statement
- Groovy - Switch Statement
- Groovy - Loops
- Groovy - For Loop
- Groovy - For-in Loop
- Groovy - While Loop
- Groovy - Do While Loop
- Groovy - Break Statement
- Groovy - Continue Statement
Groovy File Handling
- Groovy - File I/O
- Java - Create a File
- Java - Write to File
- Java - Append to File
- Java - Read Files
- Java - Delete Files
- Java - File Properties
- Java - File Existence and Type
- Java - File Size
- Java - File Permissions
- Java - Directories
- Java - Listing Directories
- Java - Filtering Files/Directories
- Java - Deleting Directories
- Java - Renaming Files/Directories
Groovy Error & Exceptions
- Groovy - Exception Handling
- Groovy - try-catch Block
- Groovy - try-with-resources
- Groovy - Multi-catch Block
- Groovy - Nested try Block
- Groovy - Finally Block
- Groovy - throw Exception
- Groovy - Exception Propagation
- Groovy - Built-in Exceptions
- Groovy - Custom Exception
Groovy Multithreading
- groovy - Multithreading
- groovy - Thread Life Cycle
- groovy - Creating a Thread
- groovy - Starting a Thread
- groovy - Joining Threads
- groovy - Naming Thread
- groovy - Thread Scheduler
- groovy - Thread Pools
- groovy - Main Thread
- groovy - Thread Priority
- groovy - Daemon Threads
- groovy - Shutdown Hook
Groovy Synchronization
- groovy - Synchronization
- groovy - Block Synchronization
- groovy - Static Synchronization
- groovy - Inter-thread Communication
- groovy - Thread Deadlock
- groovy - Interrupting a Thread
- groovy - Thread Control
- groovy - Reentrant Monitor
- Groovy - Methods
- Groovy - Methods
- Groovy - Optional parenthesis
- Groovy - Named Arguments
- Groovy - Closures as Arguments
- Groovy - Method Overloading
- Groovy - Method Scope and Visibility
- Groovy - isCase Method
- Groovy - Implicit Return
- Groovy - Variable Arguments
- Groovy - Regular Expressions
- Groovy - Regular Expressions
- Groovy - Defining Regular Expressions
- Groovy - Matcher Object
- Groovy - Regex Tasks
- Groovy - XML
- Groovy - XML
- Groovy - Parsing XML
- Groovy - Creating XML
- Groovy - Modifying XML
- Groovy - Querying XML
- Groovy - Simplified Notation
- Groovy - Closure based Querying
- Groovy - Closure based Creation
- Groovy - JSON
- Groovy - JSON
- Groovy - Parsing JSON
- Groovy - Creating JSON using JsonOutput
- Groovy - Creating JSON using JsonBuilder
- Groovy - Modifying JSON
- Groovy - Error Handling
- Groovy - Handling JSON Arrays
- Groovy - JSON Array Operations
- Groovy - JSON Objects
- Groovy - JSON Object Operations
- Groovy - Generics
- Groovy - Generics
- Groovy - Declaring Generic Types
- Groovy - Bound Type Parameters
- Groovy - Wild Cards
- Groovy - Miscellaneous
- Groovy - Object Oriented
- Groovy - Closures
- Groovy - Annotations
- Groovy - JMX
- Groovy - DSLS
- Groovy - Database
- Groovy - Builders
- Groovy - Command Line
- Groovy - Unit Testing
- Groovy - Template Engines
- Groovy - Meta Object Programming
- Groovy Useful Resources
- Groovy - Quick Guide
- Groovy - Useful Resources
- Groovy - Discussion
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.