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 6867581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:19:39+00:00 2026-05-27T03:19:39+00:00

Groovy’s MarkupBuilder has an omitNullAttributes and an omitEmptyAttributes . But StreamingMarkupBuilder doesn’t. I have

  • 0

Groovy’s MarkupBuilder has an omitNullAttributes and an omitEmptyAttributes. But StreamingMarkupBuilder doesn’t.
I have tag like that one <foo />

Could I omit them from final output?

P.S. Could I somehow use a trick from the post Omit empty attributes with groovy DOMBuilder ?

UPDATE:
Example of XML

<A>
<Header><ID>1234</ID></Header>
<Body>
<item>
<id>001</id>
<foo />
</item>
</Body>
</A>
  • 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-05-27T03:19:40+00:00Added an answer on May 27, 2026 at 3:19 am

    Right, so from the comments, it looks like you are trying to strip empty elements, not empty attributes…

    If you want to strip empty nodes from xml, you would need to read it in, find the empty ones, remove them, then write it back out…

    Like this example:

    def xml = new StringWriter().with { sw ->
      new groovy.xml.MarkupBuilder( sw ).with { mb ->
        a {
          b( 'tim' )
          foo()
        }
        sw.toString()
      }
    }
    
    def parser = new XmlParser().parseText( xml )
    
    def emptykids = parser.depthFirst().findAll { it.children().size() == 0 }
    
    emptykids.each {
      parser.remove( it )
    }
    
    new XmlNodePrinter().print( parser )
    

    However, if you want to not add empty elements to your XML when using StreamingMarkupBuilder, there is no way I know of of doing this. I guess you could re-implement the class itself to handle this, but other than that, you’re stuck…

    It should be said however that neither of the two attributes you point out in the question will make MarkupBuilder do this either. It will stop adding empty attributes, but it will add empty elements

    Now we have an example to work to:

    Try this:

    def xml = '''|<A>
                 |<Header><ID>1234</ID></Header>
                 |<Body>
                 |<item>
                 |<id>001</id>
                 |<foo />
                 |</item>
                 |</Body>
                 |</A>'''.stripMargin()
    
    def parser = new XmlParser().parseText( xml )
    
    parser.depthFirst().each { 
      if( it.children().size() == 0 ) {
        it.parent().remove( it )
      }
    }
    
    new XmlNodePrinter().print( parser )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a groovy class that has the ability to write its output to
I have a groovy script that needs a library in a jar. How do
Groovy has a concept of GStrings. I can write code like this: def greeting
We are using grails-groovy with Hibernate, I have a SQL query like this in
Groovy 1.7.5 claims to have both findResult and findAllResults . But when I try,
I have been learning Groovy for a week, but I have a problem: I
I have several Groovy beans defined within my app context file, like so: <lang:groovy
In groovy if i have the code like this : def num = 9
Groovy adds the isAllWhitespace() method to Strings, which is great, but there doesn't seem
The Groovy Process Management page mentions that: Because some native platforms only provide limited

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.