File: saxy.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 (51 lines) | stat: -rwxr-xr-x 1,265 bytes parent folder | download | duplicates (2)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env ruby

# This example demonstrates the use of the Ox.sax_parse method. An XML string
# is parsed and a list of element names if built.

# 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'

# First create a handler for the SAX callbacks. A Hash is used to collect the
# element names. This is a quick way to make sure the collected names are
# unique. Only the start_element is implemented as that is all that is needed
# to collect names. There is no need to inherit from Ox::Sax but tht class
# includes the private version of all the methods that can be made publis.
class Saxy
  def initialize
    # super
    @names = {}
  end

  def names
    @names.keys
  end

  def start_element(name)
    @names[name] = nil
  end
end

# The XML can be a string or a IO instance.
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>
}
# Create an instance of the handler.
handler = Saxy.new

Ox.sax_html(handler, xml)

puts "element names: #{handler.names}"