Last active
November 30, 2021 18:18
-
-
Save cmaggiulli/dbc375a4e7a3fe5a965ae927ec753bf0 to your computer and use it in GitHub Desktop.
Sort XML attribute value alphabetically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def xmlStr = ''' | |
| <solutions> | |
| <solution solutionName="XYZ.1052"> | |
| <settings/> | |
| </solution> | |
| <solution solutionName="XYZ.1012"> | |
| <settings/> | |
| </solution> | |
| <solution solutionName="XYZ.1053"> | |
| <settings/> | |
| </solution> | |
| </solutions> | |
| ''' | |
| def xml = new XmlParser().parseText(xmlStr) | |
| xml.solution['@solutionName'].collect{it}.sort().eachWithIndex{ name, index -> | |
| xml.solution[index].@solutionName = name | |
| } | |
| new XmlNodePrinter(new PrintWriter(new FileWriter('sorted.xml'))).print(xml) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment