131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# File 'lib/formatted_string/formats/xml.rb', line 131
def to_hash(options={})
options = XML_OPTIONS.merge(options)
stack = []
parser = REXML::Parsers::BaseParser.new(self)
while true
event = parser.pull
case event[0]
when :end_document
break
when :end_doctype, :start_doctype
when :start_element
stack.push REXMLUtilityNode.new(event[1], event[2])
when :end_element
if stack.size > 1
temp = stack.pop
stack.last.add_node(temp)
end
when :text, :cdata
stack.last.add_node(event[1]) unless event[1].strip.length == 0
end
end
data = stack.pop
data = data.to_hash if data.respond_to?(:to_hash)
if data.respond_to?(:crawl)
if options[:report_nil]
data.crawl {|h,k,v| h[k] = nil if v == {} || v == {'nil' => 'true'}; v}
else
data.crawl {|h,k,v| h[k] = nil if v == {}; v}
end
end
data
end
|