Skip to content

Instantly share code, notes, and snippets.

@rickhull
Created September 15, 2010 17:31
Show Gist options
  • Select an option

  • Save rickhull/581097 to your computer and use it in GitHub Desktop.

Select an option

Save rickhull/581097 to your computer and use it in GitHub Desktop.
<?xml version="1.0"?>
<test>"'&amp;&lt;&gt;</test>
Now monkey patching
<?xml version="1.0"?>
<test>"'&amp;&lt;&gt;</test>
<!-- Notice there is no XML entity substitution for the single- and double-quotes -->
require 'rubygems'
require 'nokogiri'
# I want to have Nokogiri make the XML entity substitutions for all the characters "'&<>
#
puts Nokogiri::XML::Builder.new { |xml| xml.test %q("'&<>) }.to_xml
puts "Now monkey patching"
class Nokogiri::XML::Node
alias old_encode_special_chars encode_special_chars
def encode_special_chars(string)
old_encode_special_chars(string).gsub("'", "&apos;").gsub('"', "&quot;")
end
end
puts Nokogiri::XML::Builder.new { |xml| xml.test %q("'&<>) }.to_xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment