File: hashi.rb

package info (click to toggle)
ruby-ox 2.14.23-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,504 kB
  • sloc: xml: 39,683; ansic: 9,626; ruby: 6,441; sh: 47; makefile: 2
file content (38 lines) | stat: -rwxr-xr-x 1,067 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env ruby

# This example demonstrates the use of Ox.load using the :hash and
# :hash_no_attrs modes.

# Use the current repo if run from the examples directory.
ox_dir = File.dirname(File.dirname(File.expand_path(__FILE__)))
$LOAD_PATH << File.join(ox_dir, 'ext')
$LOAD_PATH << File.join(ox_dir, 'lib')

require 'ox'

# load or use this sample string.
xml = %{
<?xml version="1.0"?>
<Park.Animal>
  <type>mutant</type>
  <friends type="Hash">
    <i>5</i>
    <Park.Animal>
      <type>dog</type>
    </Park.Animal>
  </friends>
</Park.Animal>
}

doc = Ox.load(xml, mode: :hash)
puts "as hash with Symbol element names: #{doc}"

# Load the XML and convert to a Hash. By default element names are
# symbolized. By using the :symbolize_keys option and setting it to false the
# element names will be strings.
doc = Ox.load(xml, mode: :hash, symbolize_keys: false)
puts "as hash with String element names: #{doc}"

# The :hash_no_attrs mode leaves attributes out of the resulting Hash.
doc = Ox.load(xml, mode: :hash_no_attrs)
puts "as hash_no_attrs: #{doc}"