Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8739567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:57:48+00:00 2026-06-13T10:57:48+00:00

I am converting a Map to XML using the Groovy MarkupBuilder . This Map

  • 0

I am converting a Map to XML using the Groovy MarkupBuilder. This Map can contain simple key/value pairs, other Maps, or Lists of Maps. I am piggybacking from the code here.

import groovy.xml.MarkupBuilder

def map = [
    key1:'value1',
    key2:'value2',
    nestedMap : [
        key1:'bar1',
        key2:'bar2'
    ],
    select : [
        [option:'foo1'],
        [option:'foo2']
    ]
]

Closure renderMap( Map map ){
    return { 
        for ( entry in map ){
            switch( entry.value.getClass() ){
                case Map :
                    "${entry.key}" renderMap( entry.value )
                break
                case List:
                    entry.value.collect { listEntry ->
                        "${entry.key}" renderMap( listEntry )
                    }
                    break
                default :
                     "${entry.key}"( "${entry.value}" )
                break
            }
        }
    }
}

StringWriter writer = new StringWriter()
new MarkupBuilder(writer).root renderMap(map)

println writer.toString()

This part I’m concerned about prints out:

  <select>
    <option>foo1</option>
  </select>
  <select>
    <option>foo2</option>
  </select>

However, I am wondering if there is a way to get the select to encapsulate both of the options, like so:

<select>
    <option>foo1</option>
     <option>foo2</option>
  </select>

I’ve tried playing around with the placement of the key, but to no avail. Am I going about this all wrong, or should I not be using the builder?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:57:49+00:00Added an answer on June 13, 2026 at 10:57 am

    I think this will do what you want. The first two overloads take a map or a collection, and return a composed closure that can be passed to the builder method of the enclosing element to add the contents of the map or collection to the builder.

    The third is a fallback, and just returns its arguments so they can be passed to the builder method. This handles the strings, but you could also pass it a closure if you want. I replaced the second option element in the map you provided as an example of that.

    ComposedClosure was added in Groovy 1.8, so this won’t work in earlier versions.

    import groovy.xml.MarkupBuilder
    
    Closure buildxml(final Map map)
    {
        final compose = { f, tag, content -> f >> { "$tag"(buildxml(content)) } }
        return map.inject(Closure.IDENTITY, compose)
    }
    
    Closure buildxml(final Collection col)
    {
        final compose = { f, content -> f >> buildxml(content) }
        return col.inject(Closure.IDENTITY, compose)
    }
    
    def buildxml(final content)
    {
        return content
    }
    
    def map = [
        key1:'value1',
        key2:'value2',
        nestedMap : [
            key1:'bar1',
            key2:'bar2'
        ],
        select : [
            [option:'foo1'],
            { option('foo2') },
        ],
    ]
    
    final writer  = new StringWriter()
    final builder = new MarkupBuilder(writer)
    
    builder.root buildxml(map)
    
    assert writer as String == '''\
    <root>
      <key1>value1</key1>
      <key2>value2</key2>
      <nestedMap>
        <key1>bar1</key1>
        <key2>bar2</key2>
      </nestedMap>
      <select>
        <option>foo1</option>
        <option>foo2</option>
      </select>
    </root>'''.stripIndent()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just read this answer Converting upper-case string into title-case using Ruby . There
I can see that it's possible to write functions like map/sortBy/findIndex and some other
Coding Platform: ASP.NET C# I have an XML like this. <Items> <Map id=35> <Terrains>
I have a Map Map<Integer, List> with key, values. I need to convert this
I am converting absolute file-system path to relative path using following code. public static
I'm using Json-Simple to write a config file using JSon-Simple lib, but I'm having
What is Google map's spatial reference system using when you enter a lat, long
I'm working with a service which returns JSON which can be converted to Map
Currently working on a game, and is converting it to retina. I am using
I am converting my web application to web servcies using CXF. My web application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.