I am trying to print formatted xml to a file but my XmlNodePrinter just prints a blank file. The xml object that I am passing in is populated correctly, I think. I can print it using a StreamingMarkupBuilder but it is formatted all on a single line. I am not sure why I cannot use the XmlNodePrinter. Here’s the relevant part of my code.
The goal of the code is to modify an xml configuration file. I have to do a find/replace on a certain permission.
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.@type.text().equals("RoleManagement")
}.each {
it.@type = "AdminRoleManagement"
}
}
String filename = "output.xml"
new XmlNodePrinter(new PrintWriter(filename)).print(root)
Thanks
I believe
XmlNodePrinterrequires aNoderather than aGPathResultXmlSlurper.parsereturnsGPathResultSo the obvious solution is to use
XmlParserinstead ofXmlSlurperOr, you can use
StreamingMarkupBuilderand do: