Project

ffi-expat

0.0
No commit activity in last 3 years
No release in over 3 years
ffi-expat provides a very thin wrapper around expat using the Ruby FFI library.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 0
 Project Readme

ffi-expat

ffi-expat is a Ruby FFI wrapper for the expat XML parsing library.

Installation

[sudo] gem install ffi-zlib

SAMPLE USAGE

ffi-expat is a thin wrapper around the expat calls so using ffi-expat in Ruby is similar to the way you would use expat in C (modulo the obious language specifics).

Here's a simple example which counts the number of each start tags:

require "rubygems"
require "ffi/expat"

class Handler
	attr_reader :starts
    def initialize
        @starts = Hash.new
    end
    def start_elem(parser, tag, attrs)
        if @starts.has_key?(tag)
            @starts[tag] += 1
        else
            @starts[tag] = 1
        end
    end
end

xml = File.read("test.xml")
handler = Handler.new
parser = FFI::Expat.XML_ParserCreate(nil)
FFI::Expat.XML_SetStartElementHandler(parser, handler.method(:start_elem))
FFI::Expat.XML_Parse(parser, xml, xml.length, true)
FFI::Expat.XML_ParserFree(parser)

handler.starts.each do |tag, count|
	puts "#{tag}: #{count}"
end

AUTHORS

Luc Heinrich luc@honk-honk.com

LICENSE

MIT.