Ruby XML

From Schmid.wiki
Jump to: navigation, search

Parse XML stream:

require "rexml/document"
require "rexml/streamlistener"

class MyListener
    include REXML::StreamListener
    def tag_start(name, attrs)
        case name
        when "a"

            # output href attribute of all <a> tags
            puts attrs["href"]
        end
    end
end

REXML::Document.parse_stream(File.new(ARGV[0]),MyListener.new)
Personal tools