Module: XMLUtil

Defined in:
lib/ec2/amitools/xmlutil.rb

Overview

Module containing utility XML manipulation functions.

Class Method Summary collapse

Class Method Details

.escape(xml) ⇒ Object

Trivially escape the XML string xml, by making the following substitutions:

  • & for &

  • < for &lt;

  • > for &gt;

Return the escaped XML string.



34
35
36
37
38
39
# File 'lib/ec2/amitools/xmlutil.rb', line 34

def XMLUtil::escape( xml )
  escaped_xml = xml.gsub( '&', '&amp;' )
  escaped_xml.gsub!( '<', '&lt;' )
  escaped_xml.gsub!( '>', '&gt;' )
  return escaped_xml
end

.get_xml(xml_data, element_name) ⇒ Object

Extract the string representation of the specified XML element name element_name from the XML string xml_data



18
19
20
21
22
23
24
25
# File 'lib/ec2/amitools/xmlutil.rb', line 18

def XMLUtil.get_xml(xml_data, element_name)
  start_tag = '<'+element_name+'>'
  end_tag = '</'+element_name+'>'
  return nil if (start_idx = xml_data.index(start_tag)).nil?
  return nil if (end_idx = xml_data.index(end_tag)).nil?
  end_idx += end_tag.size - 1
  xml_data[start_idx..end_idx]
end

.unescape(escaped_xml) ⇒ Object

Trivially unescape the escaped XML string escaped_xml, by making the following substitutions:

  • &amp; for &

  • &lt; for <

  • &gt; for >

Return the XML string.



49
50
51
52
53
54
# File 'lib/ec2/amitools/xmlutil.rb', line 49

def XMLUtil::unescape( escaped_xml )
  xml = escaped_xml.gsub( '&lt;', '<' )
  xml.gsub!( '&gt;', '>' )
  xml.gsub!( '&amp;', '&' )
  return xml
end