Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/murakmii/nokogireader
DSL for parsing xml using Nokogiri::XML::Reader
https://github.com/murakmii/nokogireader
Last synced: 8 days ago
JSON representation
DSL for parsing xml using Nokogiri::XML::Reader
- Host: GitHub
- URL: https://github.com/murakmii/nokogireader
- Owner: murakmii
- License: mit
- Created: 2016-08-12T15:01:36.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-08-15T11:55:28.000Z (over 8 years ago)
- Last Synced: 2024-12-26T10:09:46.253Z (16 days ago)
- Language: Ruby
- Size: 13.7 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Nokogireader
DSL for parsing xml using Nokogiri::XML::Reader
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'nokogireader'
```## Example
```rb
class Rss2 < Nokogireader::Reader
element :rss, attr: [:version] do
element :channel do
elements :item do
element :title
element :link
end
end
end
enddata = Rss2.new.read(File.open('rss2.xml'))
puts "Version: #{data.rss.attributes[:version]}"
puts "Items: #{data.rss.channel.item.size}"
data.rss.channel.item.each do |item|
puts " > #{item.title.text}"
end
```You can use ``after`` callback and ``dont_store_data`` to reduce memory usage.
```rb
class Rss2 < Nokogireader::Reader
element :rss, attr: [:version] do
element :channel do
elements :item do
dont_store_data
element :title
element :linkafter do |reader, item|
puts "Title: #{item.title.text}"
end
end
end
end
enddata = Rss2.new.read(File.open('rss2.xml'))
puts "Items: #{data.rss.channel.item.size}" # => 0. If specified "dont_store_data", data isn't stored.
```## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/bonono/nokogireader.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).