Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
xml
xml
<truck id='ABC123'>
  <box country='Australia'>
    <box state='QLD' country='Australia'>
      <book title='Groovy in Action' author='Dierk König et al' />
      <book title='Groovy in Action' author='Dierk König et al' />
      <book title='Groovy for VBA Macro writers' />
    </box>
    <box state='NSW' country='Australia'>
      <box city='Albury' state='NSW' country='Australia'>
        <book title='Groovy in Action' author='Dierk König et al' />
        <book title='Groovy for Fortran Programmers' />
      </box>
      <box city='Sydney' state='NSW' country='Australia'>
        <book title='Groovy in Action' author='Dierk König et al' />
        <book title='Groovy for COBOL Programmers' />
      </box>
    </box>
  </box>
  <box country='USA'>
    <box state='CA' country='USA'>
      <book title='Groovy in Action' author='Dierk König et al' />
      <book title='Groovy for Ruby programmers' />
    </box>
  </box>
  <box country='Germany'>
    <box city='Berlin' country='Germany'>
      <book title='Groovy in Action' author='Dierk König et al' />
      <book title='Groovy for PHP programmers' />
    </box>
  </box>
  <box country='UK'>
    <box city='London' country='UK'>
      <book title='Groovy in Action' author='Dierk König et al' />
      <book title='Groovy for Haskel programmers' />
    </box>
  </box>
</truck>
<truck id='ABC123'>
  <box country='Germany'>
    <box city='Berlin' country='Germany'>
      <book title='Groovy in Action' author='Dierk König et al' />
      <book title='Groovy for PHP programmers' />
    </box>
  </box>
  <box country='UK'>
    <box city='London' country='UK'>
      <book title='Groovy in Action' author='Dierk König et al' />
      <book title='Groovy for Haskel programmers' />
    </box>
  </box>
</truck>
<truck id='DEF123'>
  <box language='Haskel' city='London' country='UK'>
    <box language='Haskel' city='London' country='UK'>
      <box language='Haskel' city='London' country='UK'>
        <box language='Haskel' city='London' country='UK'>
          <box language='Haskel' city='London' country='UK'>
            <box language='Haskel' city='London' country='UK'>
              <box language='Haskel' city='London' country='UK'>
                <box language='Haskel' city='London' country='UK'>
                  <box language='Haskel' city='London' country='UK'>
                    <box country='UK'>
                      <box city='London' country='UK'>
                        <book title='Groovy in Action' author='Dierk König et al' />
                        <book title='Groovy for Haskel programmers' />
                      </box>
                    </box>
                  </box>
                </box>
              </box>
            </box>
          </box>
        </box>
      </box>
    </box>
  </box>
  <box country='South Africa'>
    <book title='Groovy in Action' author='Dierk König et al' />
    <book title='Groovy in Action' author='Dierk König et al' />
    <book title='Groovy in Action' author='Dierk König et al' />
    <book title='Groovy in Action' author='Dierk König et al' />
    <book title='Groovy in Action' author='Dierk König et al' />
    <book title='Groovy in Action' author='Dierk König et al' />
    <book title='Groovy in Action' author='Dierk König et al' />
    <book title='Groovy in Action' author='Dierk König et al' />
    <book title='Groovy in Action' author='Dierk König et al' />
    <book title='Groovy in Action' author='Dierk König et al' />
  </box>
  <box country='UK' />
</truck>
<truck id='GHI123'>
  <box country='UK'>
    <book title='Groovy in Action' author='Dierk König et al' />
  </box>
</truck>
<truck id='GHI123'>
  <box country='The United Kingdom of Great Britain and Northern Ireland'>
    <book title='Groovy in Action' author='Dierk König et al' />
  </box>
</truck>
<truck id='GHI123'>
  <box state='CA' country='USA'>
    <book title='Groovy in Action' author='Dierk König et al' />
  </box>
</truck>
<truck id='GHI123'>
  <box state='The State of Rhode Island and Providence Plantations' country='USA'>
    <book title='Groovy in Action' author='Dierk König et al' />
  </box>
</truck>

Things to be careful about when using markup builders is not to overlap variables you currently have in scope. The following is a good example

Code Block

import groovy.xml.MarkupBuilder



def book = "MyBook"


def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.shelf() {
	book(name:"Fight Club") {
		
	}
}

println writer.toString()

When run this will actually get the error

Code Block

aught: groovy.lang.MissingMethodException: No signature of method: java.lang.String.call() is applicable for argument types: (java.util.LinkedHashMap, HelloWorld$_run_closure1_closure2) values: {["name":"Fight Club"], 

This is because we have a variable above called book, then we are trying to create an element called book using the markup. Markups will always honor for variables/method names in scope first before assuming something should be interpreted as markup. But wait, we want a variable called book AND we want to create an xml element called book! No problem, use delegate variable.

Code Block

import groovy.xml.MarkupBuilder



def book = "MyBook"


def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.shelf() {
	delegate.book(name:"Fight Club") {
		
	}
}

println writer.toString()