How do you remove attributes from a node using Groovy's XMLSlurper and GPathResult?
Asked Answered
S

2

7

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

Seismic answered 20/12, 2011 at 22:25 Comment(0)
E
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.

Estancia answered 20/12, 2011 at 22:41 Comment(1)
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
S
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()
}
Seismic answered 20/12, 2011 at 23:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.