Last active
December 20, 2015 01:09
-
-
Save bootstraponline/6047083 to your computer and use it in GitHub Desktop.
XML to txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# gem install nokogiri escape_utils | |
text = '' | |
reader = Nokogiri::XML::Reader(File.open(file)) | |
reader.each do |n| | |
text += ' ' + n.value + ' ' if n.value? | |
end | |
# double unescape | |
text = EscapeUtils.unescape_html text | |
text = EscapeUtils.unescape_html text | |
# add return before >>> | |
text.gsub!(/([^>])(>+)/, "\\1\n\\2") | |
text.gsub!(/ +/, ' ') # replace multi-space with single space | |
File.open(file.path + '.txt', 'w') { |f| f.write text } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment