I have a gradle.build
where I am trying to:
XmlSlurper
to update an attribute in the read XML fileThe third step only works if I write the modified XML to a new non-existing XML file, but not the originally parsed XML file.
What is the simplest way to write the modified XML to the originally parsed XML file?
My code so far:
def inFile = file('file.xml')
def outFile = file('_file.xml')
def xml = new XmlSlurper().parse(inFile)
// update xml code here
def outBuilder = new StreamingMarkupBuilder()
def outWriter = outFile.newWriter()
XmlUtil.serialize(outBuilder.bind{ mkp.yield xml }, outWriter)
I would like outFile
to be file.xml
so that it overwrites the original XML file.
What happens if you do:
def inFile = file( 'file.xml' )
def xml = new XmlSlurper().parse( inFile )
xml.appendNode {
haha( 'tim_yates' )
}
inFile.withWriter { outWriter ->
XmlUtil.serialize( new StreamingMarkupBuilder().bind{ mkp.yield xml }, outWriter )
}
Is it just not written? (seems to work for me)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With