Would someone look at this example and annotate what is wrong,
Thank you, John
====================test-xml2.rb==============================
require 'rexml/streamlistener'
class ParcelHistoryListener
include StreamListener
def tag_name
puts tag_name
end
end
Document.parse_stream(
File.new('parcel.xml'),
ParcelHistoryListener.new
)
================= parcel.xml =============================
<?xml version="1.0" encoding="UTF-8"?>
<book name="Directory">
<person surname="Musterman" name="Max">
<address?
<street>2323 Any Street</street>
<postal-code>12345</postal-code>
<city>AnyWhere</city>
</address>
<position longitude="12.15" latitude="56.123"/>
</person>
<person surname="Musterman" name="John">
<address>
<street>2323 Any Street</street>
<postal-code>12345</postal-code>
<city>AnyWhere</city></address>
<position longitude="12.15" latitude="56.123"/>
</person>
</book>
#Generates the followwing error:
# C:/0Ruby/0/XML/xml-stream.rb:5: uninitialized constant
ParcelHistoryListener::StreamListener (NameError)
There is no StreamListener in scope ParcelHistoryListener. You want
REXML::StreamListener.
robret
I don't see what you are trying to do with
> def tag_name
> puts tag_name
> end
Basically, this is an infinite recursive procedure. The tag_name after
puts will be resolved by calling the method tag_name again. I think you
want something like:
def start_tag(tag_name, attrs)
puts tag_name
end