Class XmlSimple
In: vendor/rails/activesupport/lib/active_support/core_ext/hash/conversions.rb
vendor/rails/activesupport/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb
Parent: Object

Easy API to maintain XML (especially configuration files).

Methods

Included Modules

REXML

Classes and Modules

Class XmlSimple::Cache

Constants

KNOWN_OPTIONS = { 'in' => %w( keyattr keeproot forcecontent contentkey noattr searchpath forcearray suppressempty anonymoustag cache grouptags normalisespace normalizespace variables varattr keytosymbol ), 'out' => %w( keyattr keeproot contentkey noattr rootname xmldeclaration outputfile noescape suppressempty anonymoustag indent grouptags noindent )   Declare options that are valid for xml_in and xml_out.
DEF_KEY_ATTRIBUTES = []   Define some reasonable defaults.
DEF_ROOT_NAME = 'opt'
DEF_CONTENT_KEY = 'content'
DEF_XML_DECLARATION = "<?xml version='1.0' standalone='yes'?>"
DEF_ANONYMOUS_TAG = 'anon'
DEF_FORCE_ARRAY = true
DEF_INDENTATION = ' '
DEF_KEY_TO_SYMBOL = false

Public Class methods

Creates and initializes a new XmlSimple object.

defaults:Default values for options.

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb, line 128
128:   def initialize(defaults = nil)
129:     unless defaults.nil? || defaults.instance_of?(Hash)
130:       raise ArgumentError, "Options have to be a Hash."
131:     end
132:     @default_options = normalize_option_names(defaults, (KNOWN_OPTIONS['in'] + KNOWN_OPTIONS['out']).uniq)
133:     @options = Hash.new
134:     @_var_values = nil
135:   end

This is the functional version of the instance method xml_in.

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb, line 201
201:   def XmlSimple.xml_in(string = nil, options = nil)
202:     xml_simple = XmlSimple.new
203:     xml_simple.xml_in(string, options)
204:   end

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/hash/conversions.rb, line 22
22:   def self.xml_in_string(string, options = nil)
23:     new.xml_in_string(string, options)
24:   end

This is the functional version of the instance method xml_out.

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb, line 257
257:   def XmlSimple.xml_out(hash, options = nil)
258:     xml_simple = XmlSimple.new
259:     xml_simple.xml_out(hash, options)
260:   end

Public Instance methods

Converts an XML document in the same way as the Perl module XML::Simple.

string:XML source. Could be one of the following:
  • nil: Tries to load and parse ’<scriptname>.xml’.
  • filename: Tries to load and parse filename.
  • IO object: Reads from object until EOF is detected and parses result.
  • XML string: Parses string.
options:Options to be used.

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb, line 149
149:   def xml_in(string = nil, options = nil)
150:     handle_options('in', options)
151: 
152:     # If no XML string or filename was supplied look for scriptname.xml.
153:     if string.nil?
154:       string = File::basename($0)
155:       string.sub!(/\.[^.]+$/, '')
156:       string += '.xml'
157: 
158:       directory = File::dirname($0)
159:       @options['searchpath'].unshift(directory) unless directory.nil?
160:     end
161: 
162:     if string.instance_of?(String)
163:       if string =~ /<.*?>/m
164:         @doc = parse(string)
165:       elsif string == '-'
166:         @doc = parse($stdin.readlines.to_s)
167:       else
168:         filename = find_xml_file(string, @options['searchpath'])
169: 
170:         if @options.has_key?('cache')
171:           @options['cache'].each { |scheme|
172:             case(scheme)
173:             when 'storable'
174:               content = @@cache.restore_storable(filename)
175:             when 'mem_share'
176:               content = @@cache.restore_mem_share(filename)
177:             when 'mem_copy'
178:               content = @@cache.restore_mem_copy(filename)
179:             else
180:               raise ArgumentError, "Unsupported caching scheme: <#{scheme}>."
181:             end
182:             return content if content
183:           }
184:         end
185:         
186:         @doc = load_xml_file(filename)
187:       end
188:     elsif string.kind_of?(IO) || string.kind_of?(StringIO)
189:       @doc = parse(string.readlines.to_s)
190:     else
191:       raise ArgumentError, "Could not parse object of type: <#{string.type}>."
192:     end
193: 
194:     result = collapse(@doc.root)
195:     result = @options['keeproot'] ? merge({}, @doc.root.name, result) : result
196:     put_into_cache(result, filename)
197:     result
198:   end

Same as xml_in but doesn‘t try to smartly shoot itself in the foot.

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/hash/conversions.rb, line 9
 9:   def xml_in_string(string, options = nil)
10:     handle_options('in', options)
11: 
12:     @doc = parse(string)
13:     result = collapse(@doc.root)
14: 
15:     if @options['keeproot']
16:       merge({}, @doc.root.name, result)
17:     else
18:       result
19:     end
20:   end

Converts a data structure into an XML document.

ref:Reference to data structure to be converted into XML.
options:Options to be used.

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb, line 212
212:   def xml_out(ref, options = nil)
213:     handle_options('out', options)
214:     if ref.instance_of?(Array)
215:       ref = { @options['anonymoustag'] => ref }
216:     end
217: 
218:     if @options['keeproot']
219:       keys = ref.keys
220:       if keys.size == 1
221:         ref = ref[keys[0]]
222:         @options['rootname'] = keys[0]
223:       end
224:     elsif @options['rootname'] == ''
225:       if ref.instance_of?(Hash)
226:         refsave = ref
227:         ref = {}
228:         refsave.each { |key, value|
229:           if !scalar(value)
230:             ref[key] = value
231:           else
232:             ref[key] = [ value.to_s ]
233:           end
234:         }
235:       end
236:     end
237: 
238:     @ancestors = []
239:     xml = value_to_xml(ref, @options['rootname'], '')
240:     @ancestors = nil
241: 
242:     if @options['xmldeclaration']
243:       xml = @options['xmldeclaration'] + "\n" + xml
244:     end
245: 
246:     if @options.has_key?('outputfile')
247:       if @options['outputfile'].kind_of?(IO)
248:         return @options['outputfile'].write(xml)
249:       else
250:         File.open(@options['outputfile'], "w") { |file| file.write(xml) }
251:       end
252:     end
253:     xml
254:   end

[Validate]