Module: XMLCodec::XMLUtils
- Defined in:
- lib/XMLUtils.rb
Class Method Summary collapse
-
.count_elements(path, filename) ⇒ Object
Count the number of elements that correspond to a given xpath in a file.
-
.create_close_tag(name) ⇒ Object
Create a close tag.
-
.create_open_tag(name, attrs) ⇒ Object
Create an open tag.
-
.element_exists(path, filename) ⇒ Object
Test if a given xpath exists in the file.
-
.escape_xml(string) ⇒ Object
Escape a string so that it can be included in a XML document.
-
.getdoc(filename) ⇒ Object
Gets the Nokogiri DOM for a given filename that must be a XML file.
-
.select_path(path, filename) ⇒ Object
Get a xpath from a file.
-
.select_path_doc(path, doc) ⇒ Object
Get a xpath from a Nokogiri::XML::Document.
Class Method Details
.count_elements(path, filename) ⇒ Object
Count the number of elements that correspond to a given xpath in a file.
17 18 19 |
# File 'lib/XMLUtils.rb', line 17 def self.count_elements(path, filename) getdoc(filename).xpath(path).size end |
.create_close_tag(name) ⇒ Object
Create a close tag.
47 48 49 |
# File 'lib/XMLUtils.rb', line 47 def self.create_close_tag(name) "</"+name.to_s+">" end |
.create_open_tag(name, attrs) ⇒ Object
Create an open tag.
39 40 41 42 43 44 |
# File 'lib/XMLUtils.rb', line 39 def self.create_open_tag(name, attrs) str = "<"+name attrs.each {|name, value| str << " #{name}='#{value}'"} str << ">" str end |
.element_exists(path, filename) ⇒ Object
Test if a given xpath exists in the file.
22 23 24 |
# File 'lib/XMLUtils.rb', line 22 def self.element_exists(path, filename) count_elements(path,filename)>0 end |
.escape_xml(string) ⇒ Object
Escape a string so that it can be included in a XML document
52 53 54 |
# File 'lib/XMLUtils.rb', line 52 def self.escape_xml(string) Nokogiri::XML::Text.new(string, Nokogiri::XML::Document.new).to_s end |
.getdoc(filename) ⇒ Object
Gets the Nokogiri DOM for a given filename that must be a XML file.
12 13 14 |
# File 'lib/XMLUtils.rb', line 12 def self.getdoc(filename) Nokogiri::XML::Document.parse File.new(filename, 'r') end |
.select_path(path, filename) ⇒ Object
Get a xpath from a file.
34 35 36 |
# File 'lib/XMLUtils.rb', line 34 def self.select_path(path, filename) select_path_doc(path, getdoc(filename)) end |
.select_path_doc(path, doc) ⇒ Object
Get a xpath from a Nokogiri::XML::Document.
27 28 29 30 31 |
# File 'lib/XMLUtils.rb', line 27 def self.select_path_doc(path, doc) els = doc.xpath(path) return "" if !els[0] els[0].children.to_s end |