Module: XML
- Defined in:
- lib/webget_ruby_ramp/xml.rb
Overview
XML extensions
Class Method Summary collapse
-
.load_attributes(dirpath, xpath) ⇒ Object
Sugar to load attributes from a file.
-
.load_attributes_hash(dirpath, xpath) ⇒ Object
Sugar to load attributes hash from a file.
-
.load_dir(*dirpaths) ⇒ Object
Specify one or more directory patterns and pass each XML file in the matching directories to a block.
-
.load_elements(dirpath, xpath) ⇒ Object
Sugar to load elements from a file.
-
.strip_all(xml_text) ⇒ String
Santize dirty xml by removing unprintables, bad tags, comments, and generally anything else we might need to enable the XML parser to handle a dirty document.
-
.strip_attributes(xml_text) ⇒ String
Strip out all attributes from the xml text’s tags.
-
.strip_comments(xml_text) ⇒ String
Strip out all comments from the xml text.
-
.strip_microsoft(xml_text) ⇒ String
Strip out all microsoft proprietary codes.
-
.strip_unprintables(xml_text) ⇒ String
Strip out all unprintable characters from the input string.
Class Method Details
.load_attributes(dirpath, xpath) ⇒ Object
Sugar to load attributes from a file.
56 57 58 59 60 |
# File 'lib/webget_ruby_ramp/xml.rb', line 56 def XML.load_attributes(dirpath,xpath) XML.load_elements(dirpath,xpath){|elem| yield elem.attributes } end |
.load_attributes_hash(dirpath, xpath) ⇒ Object
Sugar to load attributes hash from a file.
67 68 69 70 71 |
# File 'lib/webget_ruby_ramp/xml.rb', line 67 def XML.load_attributes_hash(dirpath,xpath) XML.load_elements(dirpath,xpath){|elem| yield elem.attributes.to_hash } end |
.load_dir(*dirpaths) ⇒ Object
Specify one or more directory patterns and pass each XML file in the matching directories to a block.
See [Dir#glob](www.ruby-doc.org/core/classes/Dir.html#M002347) for pattern details.
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/webget_ruby_ramp/xml.rb', line 24 def XML.load_dir(*dirpaths) dirpaths=[*dirpaths.flatten] dirpaths.each do |dirpath| Dir[dirpath].sort.each do |filename| File.open(filename) do |file| doc = REXML::Document.new file yield doc end #file end #dir end #each end |
.load_elements(dirpath, xpath) ⇒ Object
Sugar to load elements from a file.
42 43 44 45 46 47 48 |
# File 'lib/webget_ruby_ramp/xml.rb', line 42 def XML.load_elements(dirpath,xpath) XML.load_dir(dirpath){|doc| doc.elements.each(xpath){|elem| yield elem } } end |
.strip_all(xml_text) ⇒ String
Santize dirty xml by removing unprintables, bad tags, comments, and generally anything else we might need to enable the XML parser to handle a dirty document.
This method calls these in order:
- XML.strip_unprintables
- XML.strip_microsoft
- XML.strip_comments
- XML.strip_attributes
91 92 93 |
# File 'lib/webget_ruby_ramp/xml.rb', line 91 def XML.strip_all(xml_text) return XML.strip_attributes(XML.strip_comments(XML.strip_microsoft(XML.strip_unprintables(xml_text)))) end |
.strip_attributes(xml_text) ⇒ String
Strip out all attributes from the xml text’s tags.
104 105 106 |
# File 'lib/webget_ruby_ramp/xml.rb', line 104 def XML.strip_attributes(xml_text) return xml_text.gsub(/<(\/?\w+).*?>/im){"<#{$1}>"} # delete attributes end |
.strip_comments(xml_text) ⇒ String
Strip out all comments from the xml text.
118 119 120 |
# File 'lib/webget_ruby_ramp/xml.rb', line 118 def XML.strip_comments(xml_text) return xml_text.gsub(/<!.*?>/im,'') end |