I need to remove the attributes from a body node in some parsed HTML (converted to XML).
How do you remove attributes from a node using Groovy's XMLSlurper and GPathResult?
Asked Answered
Call the attributes()
on the element that contains the attribute and then call remove('attr name')
as shown below.
attributes().remove('attr name')
You can read more details here.
Ah, I couldn't see the attributes() method in the documentation. Thanks for the pointer and also see the final method I came up with. –
Seismic
/**
* Remove all attributes from the root body tag
*/
def removeBodyAttributes() {
def attributeNames = bodyXml.attributes().collect {it.key}
println attributeNames
println bodyXml.attributes()
attributeNames.each {bodyXml.attributes().remove(it)}
println bodyXml.attributes()
}
© 2022 - 2024 — McMap. All rights reserved.