Removing blank nodes recursively from nokogiri object
While parsing xml using Nokogiri it contains blank nodes in parent and childre nodes it looks like this
To remove blank nodes recursively from every children and their children this code will work for you.
Now apply it the parsed nokogiri object in this code I have also removed namespaces.
Thanks for reading this blog for further help in Nokogiri you can ask me.
#<Nokogiri::XML::Text:0x2d02be8 "\n">and noblanks removes only blank nodes in parent node which become really hard when recursively fetching nodes.
To remove blank nodes recursively from every children and their children this code will work for you.
module NokoHacks
extend ActiveSupport::Concern
Nokogiri::XML::NodeSet.send :include, NokoHacks
Nokogiri::XML::Text.send :include, NokoHacks
Nokogiri::XML::Element.send :include, NokoHacks
def remove_blank_node
self.children.each do |node|
if node.blank?
node.remove
end
if node.children
node.remove_blank_node
end
end
end
end
Now apply it the parsed nokogiri object in this code I have also removed namespaces.
xml_obj = Nokogiri::XML(File.open('config/london.xml')).remove_namespaces! do |config|
config.default_xml.noblanks
end
xml_obj.children.remove_blank_node
Thanks for reading this blog for further help in Nokogiri you can ask me.
Comments
Post a Comment