File: visitor.rb

package info (click to toggle)
libxml-parser-ruby 0.5.16-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 596 kB
  • ctags: 702
  • sloc: ruby: 4,474; ansic: 1,254; xml: 542; makefile: 53
file content (29 lines) | stat: -rwxr-xr-x 702 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
#! /usr/local/bin/ruby

## Visitor sample
## 1998 by yoshidam
##
## The sample for Ruby style visitor.
## You can use "each" method as the iterator to visit all nodes,
## and can also use the other Enumerable module methods.

require 'xmltreebuilder'
require 'xmltreevisitor'
require 'xmlencoding-ja'
include XMLEncoding_ja

p = XML::SimpleTreeBuilder.new(1)
tree = p.parse($<)
tree.documentElement.normalize

tree.each_with_index do |node, index|
  print format("%03d: ", index)
  case node.nodeType
  when XML::SimpleTree::Node::ELEMENT
    print "<#{node.nodeName}>\n"
  when XML::SimpleTree::Node::DOCUMENT
    print "#DOCUMENT\n"
  else
    print "#{Uconv.u8toeuc(node.to_s).inspect}\n"
  end
end