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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:36:34+00:00 2026-06-01T05:36:34+00:00

I need to print XML that I manipulated using groovy and maintain the attribute

  • 0

I need to print XML that I manipulated using groovy and maintain the attribute order. I don’t care if it’s using XmlParser/XmlNodePrinter or XmlSlurper/StreamingMarkupBuilder. My current code is as follows:

File file = new File('input.xml')
def root = new XmlSlurper().parse(file)

def admins = root.user.findAll {it.@role.text().equals("admin")}
admins.each { admin ->
  admin.permission.findAll { it.@interface.text().equals("RoleManagement") 
  }.each {
    it.@implementation = "AdminRoleManagement"
  }
}

def smb = new StreamingMarkupBuilder().bind { mkp.yield root }
new File('output.xml').text = groovy.xml.XmlUtil.serialize(smb)

Here’s the XML that gets fed into the program

<users>
  <user username="test1234" role="admin">
    <permission interface="com.test.RoleManagement" implementation="com.test.AdminRoleManagement"/>
    <permission interface="com.test.UserAdministration" implementation="com.test.UserAdministrationImpl"/>
  </user>
</users>

After I print out the modified file, though, the interface and implementation attributes are reversed.

I already know what you’re thinking: xml attribute order does not matter. Well, my requirement from my boss is to preserve the attribute order b/c it has been that way for ages. I was actually supposed to write this parser using Java/DOM4J and I was trying to show my team something new. Any help would be appreciated. Thanks!

  • 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-01T05:36:36+00:00Added an answer on June 1, 2026 at 5:36 am

    I don’t think this is possible without writing your own code to output the XML.

    The SAX parser has no concept of attribute ordering (AFAIK), and so the order will be lost before XmlSlurper even sees the data… I found a thread on the groovy-user list which has a discussion of this, but it doesn’t seem to come up with any solutions…

    I think it’s possible with XmlParser, as that seems to maintain order:

    def xml = '''<users>
                |  <user username="test1234" role="admin">
                |    <permission interface="com.test.RoleManagement" implementation="com.test.AdminRoleManagement"/>
                |    <permission interface="com.test.UserAdministration" implementation="com.test.UserAdministrationImpl"/>
                |  </user>
                |</users>'''.stripMargin()
    
    def root = new XmlParser().parseText( xml )
    
    def admins = root.user.findAll { it.@role == "admin" }
    
    admins.each { admin ->
      admin.permission.findAll {
        it.@interface == "com.test.RoleManagement" 
      }.each {
        it.@implementation = "AdminRoleManagement"
      }
    }
    
    String output = new StringWriter().with { sw ->
      new XmlNodePrinter( new PrintWriter( sw ) ).print( root )
      sw.toString()
    }
    println output
    

    That prints:

    <users>
      <user username="test1234" role="admin">
        <permission interface="com.test.RoleManagement" implementation="AdminRoleManagement"/>
        <permission interface="com.test.UserAdministration" implementation="com.test.UserAdministrationImpl"/>
      </user>
    </users>
    

    Which at first glance seems right?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need to print out numbers 1-100 in a random order. the print statement
I need to print a receipt from my web based apps using dot matrix
I have a DateTime object that I need to print in a custom gridlike
I have an XML document that I've received from my server. I need to
So I need to build an application that will receive xml request and based
I need to print out the contents of an xml file into some txt
I have many XML files in a directory that need to sorted and merged
Very new to XSL (and XML for that matter), but I need to step
I need to print out data into a pre-printed A6 form (1/4 the size
I need to print the date in the format of mm/dd/yyyy. if the date

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.