Module: XMLUtils

Defined in:
lib/XMLUtils.rb,
lib/stream_parser.rb

Overview

This module holds generic XML utilities. The module’s methods are simple XML utilities. The module also contains XMLStreamParser, a Generic XML Stream parser whose events are the text of whole elements instead of start and end tags.

Defined Under Namespace

Classes: XMLSParserContents, XMLSParserElement, XMLStreamParser

Class Method Summary collapse

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
20
21
22
# File 'lib/XMLUtils.rb', line 17

def self.count_elements(path, filename)
  doc = getdoc(filename)
  i = 0
  REXML::XPath.each(doc, path) {|element| i+=1}
  return i
end

.create_open_tag(name, attrs) ⇒ Object

Create an open tag.



45
46
47
48
49
50
# File 'lib/XMLUtils.rb', line 45

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.



25
26
27
# File 'lib/XMLUtils.rb', line 25

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



53
54
55
56
57
58
# File 'lib/XMLUtils.rb', line 53

def self.escape_xml(string)
  t = REXML::Text.new('')
  str = ''
  t.write_with_substitution(str, string)
  str
end

.get_rexml_content(element, opts = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/XMLUtils.rb', line 86

def self.get_rexml_content(element, opts={})
  return '' if not element
  strs = []
  if element.respond_to?('value')
    return element.value || ''
  else
    if opts[:with_attrs]
      element.attributes.each {|name, value| strs << value if value != ''}
    end
    if not opts[:recursive]
      element.texts.each {|t| strs << t if t != ''}
    else  
      element.to_a.each do |e|
        t = get_rexml_content(e, opts)
        strs << t if t != ''
      end
    end
  end
  strs.join(' ')
end

.get_xpath(path, doc, opts = {}) ⇒ Object

Gets the xpath inside a given document that can either be a string or a REXML::Document

opts can have:

:multiple: fetch all the occurences of the xpath
:with_attrs: include the attribute contents in the result
:recursive: recursively include all the subelements of the matches


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/XMLUtils.rb', line 67

def self.get_xpath(path, doc, opts={})
  if doc.is_a? REXML::Document
    doc = doc
  else
    doc = REXML::Document.new(doc)
  end
  
  if opts[:multiple]
    strs = []
    REXML::XPath.each(doc.root, path) do |e|
      t = get_rexml_content(e, opts)
      strs << t if t != ''
    end
    return strs.join(' ')
  else
    return get_rexml_content(REXML::XPath.first(doc, path), opts)
  end
end

.getdoc(filename) ⇒ Object

Gets the REXML DOM for a given filename that must be a XML file.



11
12
13
14
# File 'lib/XMLUtils.rb', line 11

def self.getdoc(filename)
  file = File.new(filename, 'r')
  REXML::Document.new file
end

.select_path(path, filename) ⇒ Object

Get a xpath from a file.



40
41
42
# File 'lib/XMLUtils.rb', line 40

def self.select_path(path, filename)
  XMLUtils::select_path_doc(path, getdoc(filename))
end

.select_path_doc(path, doc) ⇒ Object

Get a xpath from a REXML document.



30
31
32
33
34
35
36
37
# File 'lib/XMLUtils.rb', line 30

def self.select_path_doc(path, doc)
  element = REXML::XPath.first(doc, path)
  return "" if not element
  if element.respond_to?("value")
    return element.value || ""
  end
  return element.text || ""
end