File: visitortest.rb

package info (click to toggle)
libxml-parser-ruby 0.6.8-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 932 kB
  • ctags: 1,525
  • sloc: ruby: 11,080; ansic: 1,958; xml: 467; makefile: 6
file content (36 lines) | stat: -rwxr-xr-x 902 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
#! /usr/local/bin/ruby

## Visitor test
## 1998 by yoshidam
##
## This sample comes from Ken MacLeod's sample of XML-Grove-0.05
## Copyright (C) 1998 Ken MacLeod

require 'xml/dom/builder'
require 'xml/dom/visitor'

class MyVisitor<XML::DOM::Visitor
  def visit_Element(element, context, *rest)
    context.push(element.nodeName)
    attrs = []
    element.attributes.each do |attr|
      attrs.push(attr.to_s)
    end
    print "#{context.join(' ')} \\\\ (#{attrs.join(' ')})\n"
    super(element, context, *rest)
    print "#{context.join(' ')} //\n"
    context.pop
  end

  def visit_ProcessingInstruction(pi, context, *rest)
    print "#{context.join(' ')} ?? #{pi.target}(#{pi.data})\n"
  end

  def visit_Text(text, context, *rest)
    value = text.nodeValue
    print "#{context.join(' ')} || #{value.dump}\n"
  end
end

doc = XML::DOM::Builder.new.parse($<.read)
doc.accept(MyVisitor.new, [])