Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jcf/vtd-xml

A fast JRuby-only XML parser
https://github.com/jcf/vtd-xml

Last synced: 1 day ago
JSON representation

A fast JRuby-only XML parser

Awesome Lists containing this project

README

        

# vtd-xml

Parse large amounts of XML quickly and easily.

**This library currently only works with JRuby.**

[![Build Status](https://travis-ci.org/jcf/vtd-xml.png?branch=master)](https://travis-ci.org/jcf/vtd-xml)
[![Code Climate](https://codeclimate.com/github/jcf/vtd-xml.png)](https://codeclimate.com/github/jcf/vtd-xml)

## Installation

Add this line to your application's Gemfile:

gem 'vtd-xml', '~> 0.0.1'

And then execute:

bundle

Or install it yourself as:

gem install vtd-xml

## Usage

With the following example XML:

``` xml



English
Chapman & Hall

5
2
3




English
George Allen & Unwin

5
4
5




French
Gallimard

4
2
3

```

### Parsing a file

``` ruby
require 'vtd-xml'

# Create a parser
parser = VTD::Xml::Parser.new 'path/to.xml'

# This shortcut does the same
parser = VTD::Xml.open 'path/to.xml'

parser.find('//book/author').each do |node|
# Iterates through each node
end

parser.find('//book/author').max_by { |node| node['sold'] }
```

### Finding a node

``` ruby
node = parser.find('//book/author[1]').first
```

### Working with attributes

``` ruby

# Accessing attributes
node['name']

node.fetch('missing', 'so use this default')
node.fetch('another-missing') { 'so call this block and use it' }

node.slice('title', 'missing')
# => {'title' => 'A Tale of Two Cities', 'missing' => nil}

node.attributes # => returns every attribute
```

### Node traversal

``` ruby
child = node.children('language').first
child.text # => "English"

node.children('rating').map do |child|
child.text.to_i
end
# => [5,2,3]
```

**See the examples directory for more.**

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request