Book Image

Mastering Gradle

Book Image

Mastering Gradle

Overview of this book

Table of Contents (17 chapters)
Mastering Gradle
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Builder


Another important feature in Groovy is Builder. Groovy Builders allow you to create complex tree-like hierarchical object structures. For example, SwingUI or XML documents can be created very easily using the DSL or Closure-like features in Groovy, with the support of the BuilderSupport class and its subclasses, MarkupBuilder and SwingBuilder.

Let's try to understand with an example. We created the Order class earlier in this chapter. Assume we have a list of orders and we want to store the details in a file called orders.xml. So every Order object in our list should be saved as a node in the XML file. Each of these Order nodes, again should contain child nodes, grand children nodes, and so on. Creating this tree-like structure can be complex if we try to implement a DOM-like parser in Java:

<orders>
  <order>
    <no>1</no>
    <description>Ordered by customer 1</description>
    <customer>
      <name firstname='Customer1' />
     ...