Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 408
Peter Kelley Avatar asked Dec 20 '11 22:12

Peter Kelley


People also ask

What is groovy XmlSlurper?

Groovy's internal XmlParser and XmlSlurper provide access to XML documents in a Groovy-friendly way that supports GPath expressions for working on the document. XmlParser provides an in-memory representation for in-place manipulation of nodes, whereas XmlSlurper is able to work in a more streamlike fashion.

How do I read XML content in groovy?

XML ParsingThe Groovy XmlParser class employs a simple model for parsing an XML document into a tree of Node instances. Each Node has the name of the XML element, the attributes of the element, and references to any child Nodes. This model is sufficient for most simple XML processing.

What is XML slurper?

XmlSlurper() Creates a non-validating and namespace-aware XmlSlurper which does not allow DOCTYPE declarations in documents. XmlSlurper(boolean validating, boolean namespaceAware) Creates a XmlSlurper which does not allow DOCTYPE declarations in documents.

What is node in groovy script?

Represents an arbitrary tree node which can be used for structured metadata or any arbitrary XML-like tree. A node can have a name, a value and an optional Map of attributes.


1 Answers

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.

like image 164
Aravind Yarram Avatar answered Nov 14 '22 22:11

Aravind Yarram