Skip to content

Instantly share code, notes, and snippets.

@TomyLobo

TomyLobo/bla.rb Secret

Created March 13, 2020 11:52
Show Gist options
  • Save TomyLobo/13b25cf663ab3bcaa96fd76c400e1ceb to your computer and use it in GitHub Desktop.
Save TomyLobo/13b25cf663ab3bcaa96fd76c400e1ceb to your computer and use it in GitHub Desktop.
require 'nokogiri'
require 'htmlentities'
# ...
class EntityReplacingVisitor
def visit(node)
if node.instance_of?(Nokogiri::XML::EntityReference)
@decoder ||= HTMLEntities.new
node.replace(@decoder.decode(node.to_xml))
return
end
node.children.each do |child|
child.accept(self)
end
end
end
xml_doc = Nokogiri::XML(xml) do |configure|
configure.strict # strict parsing
end
xml_doc.accept(EntityReplacingVisitor.new)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment