I want to extract parts of an XML file and make a note that I extracted some part in that file, like "here something was extracted".
I'm trying to do this with Nokogiri, but it seems to not really be documented on how to:
- delete all childs of a
<Nokogiri::XML::Element>
- change the
inner_text
of that complete element
Any clues?
The previous Nokogiri example set me in the right direction, but using
doc.search
left a malformed//vitamins
, so I used CSS:Which results in:
Nokogiri makes this pretty easy. Using this document as an example, the following code will find all
vitamins
tags, remove their children (and the children's children, etc.), and change their inner text to say "Children removed.":A given
food
node will go from looking like this:to this:
Here's what I'd do:
Parse some XML first:
If I want to delete a node's content, I can remove its
children
or assign nil to its content:or:
If I want to extract the text from a node for use some other way:
or:
or however else you want to mangle the text.
And, if you want to mark that you've removed the text:
You could also use an XML comment instead:
You can do it like this:
While that would remove all children within the
<note>
tag, I am not sure how to "change the inner_text" of all note elements. I thinkinner_text
is not applicable for a Nokogiri::XML::Element.