Class: AXML::XMLParser::Parser

Inherits:
AXML::XMLParser show all
Defined in:
lib/axml/xmlparser.rb

Constant Summary collapse

NotBlankText_re =
/[^\s+]+/m

Constants included from AXML

CLASS_MAPPINGS, DEFAULTS, PREFERRED, WARN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AXML::XMLParser

parse_io, parse_string

Methods included from AXML

#parse, #parse_file, #xml?

Constructor Details

#initializeParser

takes opts from AXML::parse method



93
94
95
96
97
# File 'lib/axml/xmlparser.rb', line 93

def initialize
  #@keep_blanks = opts[:keep_blanks]
  @root = AXML::El.new(nil, "root", {}, '', [])
  @cur = @root
end

Instance Attribute Details

#rootObject

returns the first node found in the document



44
45
46
# File 'lib/axml/xmlparser.rb', line 44

def root
  @root.child
end

Instance Method Details

#character(data) ⇒ Object



105
106
107
# File 'lib/axml/xmlparser.rb', line 105

def character(data)
  @cur.text << data
end

#endElement(name) ⇒ Object



109
110
111
# File 'lib/axml/xmlparser.rb', line 109

def endElement(name)
  @cur = @cur.parent
end

#set_no_keep_blanksObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/axml/xmlparser.rb', line 48

def set_no_keep_blanks
  instance_eval do
    def endElement(name)
      unless NotBlankText_re.match(@cur.text)
        @cur.text = nil
      end
      @cur = @cur.parent
    end
  end
end

#set_single_text_indices(el_name) ⇒ Object

returns text as an array for each occurence of the specified element: [start_index, num_bytes]



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/axml/xmlparser.rb', line 60

def set_single_text_indices(el_name)
  @el_name = el_name
  instance_eval do
    def startElement(name, attributes)
      text =
        if name == @el_name ; []
        else ; ''
        end
      new_el = ::AXML::El.new(@cur, name, attributes, text, [])
      @cur.add_node(new_el)
      @cur = new_el
    end

    def character(data)
      if @cur.text.is_a? Array
        @cur.text << byteIndex
      else
        @cur.text << data
      end
    end

    tmpvar = $VERBOSE ; $VERBOSE = nil
    def endElement(name)
      if @cur.text.is_a? Array
        @cur.text << (byteIndex - @cur.text.first)
      end
      @cur = @cur.parent
    end
    $VERBOSE = tmpvar
  end
end

#startElement(name, attributes) ⇒ Object



99
100
101
102
103
# File 'lib/axml/xmlparser.rb', line 99

def startElement(name, attributes)
  new_el = AXML::El.new(@cur, name, attributes, '', [])
  @cur.add_node(new_el)
  @cur = new_el
end