6

I need to remove the attributes from a body node in some parsed HTML (converted to XML).

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Peter Kelley
  • 2,350
  • 8
  • 26
  • 46

2 Answers2

5

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.

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
  • 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. – Peter Kelley Dec 20 '11 at 23:02
2
/**
 * 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()
}
Peter Kelley
  • 2,350
  • 8
  • 26
  • 46