About Hubert Ikkink

My name is Hubert A. Klein Ikkink also known as mrhaki. I work at the great IT company JDriven. Here I work on projects with Groovy & Grails, Gradle and Spring. At JDriven we focus on SpringSource technologies. All colleagues want to learn new technologies, support craftmanship and are very eager to learn. This is truly a great environment to work in.

Grails Goodness: Customize Root Element Name Collections for XML Marshalling

When we convert a List or Set to XML using the Grails XML marshalling support the name of the root element is either <list> or <set>. We can change this name by extending the org.codehaus.groovy.grails.web.converters.marshaller. xml.CollectionMarshaller. We must override the method supports() to denote the type of collection we want to customize the root element name for. And we must override the method getElementName() which returns the actual name of the root element for the List or Set.

Let’s first see the default output of a collection of Book domain classes. In a controller we have the following code:
 

package com.mrhaki.grails.sample 

class SampleController {
    def list() {
        // To force 404 when no results are found
        // we must return null, because respond method
        // checks explicitly for null value and 
        // not Groovy truth.
        respond Book.list()
    }
}

The XML output is:

<?xml version="1.0" encoding="UTF-8"?>
<list>
  <book id="1" version="0">
    <author id="1" />
    <isbn>
      0451169514
    </isbn>
    <numberOfPages>
      1104
    </numberOfPages>
    <title>
      It
    </title>
  </book>
  <book id="2" version="0">
    <author id="1" />
    <isbn>
      0307743683
    </isbn>
    <numberOfPages>
      1472
    </numberOfPages>
    <title>
      The stand
    </title>
  </book>
</list>

To change the element name list to books we add the following code to the init closure in grails-app/app/BootStrap.groovy:

// File: grails-app/conf/BootStrap.groovy

import com.mrhaki.grails.sample.Book
import grails.converters.XML
import org.codehaus.groovy.grails.web.converters.marshaller.xml.CollectionMarshaller

class BootStrap {

    def init = { servletContext ->
        // Register custom collection marshaller for List with Book instances.
        // The root element name is set to books.
        XML.registerObjectMarshaller(new CollectionMarshaller() {
            @Override
            public boolean supports(Object object) {
                // We know there is at least one result, 
                // otherwise the controller respond method
                // would have returned a 404 response code.
                object instance of List && object.first() instance of Book
            }

            @Override
            String getElementName(final Object o) {
                'books'
            }
        })
    }
}

Now when we render a list of Book instances we get the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<books>
  <book id="1" version="0">
    <author id="1" />
    <isbn>
      0451169514
    </isbn>
    <numberOfPages>
      1104
    </numberOfPages>
    <title>
      It
    </title>
  </book>
  <book id="2" version="0">
    <author id="1" />
    <isbn>
      0307743683
    </isbn>
    <numberOfPages>
      1472
    </numberOfPages>
    <title>
      The stand
    </title>
  </book>
</books>

To customize the XML marshaling output for Map collections we must subclass org.codehaus.groovy.grails.web.converters.marshaller.xml.MapMarshaller.

Code written with Grails 2.3.2.

 

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


× nine = 27



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close