File: xmliter.rb.old

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 (57 lines) | stat: -rwxr-xr-x 1,382 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
52
53
54
55
56
57
#! /usr/local/bin/ruby

require 'xmlparser'
require 'kconv'
include Kconv
require 'uconv'
include Uconv

xml = $<.gets
if xml =~ /^<\?xml\sversion=.+\sencoding=.EUC-JP./i
  xml.sub!(/EUC-JP/i, "UTF-8")
  encoding = 'EUC-JP'
elsif xml =~ /^<\?xml\sversion=.+\sencoding=.Shift_JIS./i
  xml.sub!(/Shift_JIS/i, "UTF-8")
  encoding = "Shift_JIS"
elsif xml =~ /^<\?xml\sversion=.+\sencoding=.ISO-2022-JP./i
  xml.sub!(/ISO-2022-JP/i, "UTF-8")
  encoding = "ISO-2022-JP"
end
xml += $<.read
if encoding == "EUC-JP"
  xml = euctou8(xml)
elsif encoding == "Shift_JIS"
  xml = euctou8(kconv(xml, EUC, SJIS))
elsif encoding == "ISO-2022-JP"
  xml = euctou8(kconv(xml, EUC, JIS))
end

parser = XMLParser.new
def parser.default
end

begin
  parser.parse(xml) do |type, name, data|
    case type
    when XMLParser::START_ELEM
      data.each do |key, value|
        print Uconv.u8toeuc("A#{key} CDATA #{value}\n")
      end
      print "(#{name}\n"
    when XMLParser::END_ELEM
      print Uconv.u8toeuc(")#{name}\n")
    when XMLParser::CDATA
      data.gsub!("\n", "\\n")
      print Uconv.u8toeuc("-#{data}\n")
    when XMLParser::PI
      data.gsub!("\n", "\\n")
      print Uconv.u8toeuc("?#{name} #{data}\n")
    else
      data.gsub!("\n", "\\n")
      print Uconv.u8toeuc("//#{data}\n")
    end
  end
rescue XMLParserError
  line = parser.line
  print "Parse error(#{line}): #{$!}\n"
end