File: visitor.rb

package info (click to toggle)
libxml-parser-ruby 0.6.8-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 912 kB
  • ctags: 1,523
  • sloc: ruby: 11,080; ansic: 1,958; xml: 467; makefile: 59
file content (29 lines) | stat: -rwxr-xr-x 722 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
#! /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 'xml/dom/builder'
require 'xml/dom/visitor'
require 'xml/encoding-ja'
include XML::Encoding_ja

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

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