Project

xmlcellent

0.0
No commit activity in last 3 years
No release in over 3 years
An XML parser that uses Nokogiri and dynamic methods to transform XML data into a given Ruby model
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.0.0
~> 1.6.4
>= 0

Runtime

~> 1.5.0
 Project Readme

Xmlcellent

A more excellent way to parse XML into Ruby models and objects

About

Imagine you have some XML in your application that you need to turn into a collection of models. You could do it the way you've always done it – loop through some XML, creating models as you go – or you could use Xmlcellent and stop worrying. Xmlcellent: it's a more excellent way to turn XML into something you can use.

Installation

gem install xmlcellent

Getting started

It's simple: include gem "xmlcellent" in your Gemfile, and take a look at this:

# Sample XML
<employees>
  <employee company="Sterling Cooper">
    <name>Don Draper</name>
    <description>
      <paragraph>Lorem ipsum dolor.</paragraph>
      <paragraph>Sit amet consecteteur.</paragraph>
      <paragraph>Adipiscing elit donec odio.</paragraph>
    </description>
    <hobby skill="excellent">Philandering</hobby>
  </employee>
</employees>

# Employee model
class Employee
  attr_accessor :name, :company, :description, :hobby
end

# Xmlcellent
require "xmlcellent"
Xmlcellent::Parser.define_format :employees, Employee, {
  :finder => "//employee"
  :lexicon => {
    :name => "name",
    :company => "@company"
    :description => "description",
    :hobby => lambda { |obj|
      obj.xpath("hobby").text + "Skill: #{obj.xpath("hobby/@skill")}"
    }
  }
}

Xmlcellent::Parser.parse(@xml_string)
puts Xmlcellent::Parser.results

How to use

The parser does not need to be instantiated; it can be controlled through two class methods: parse and define_format. define_format is used to describe your XML structure to the Parser. It takes three arguments:

  1. A descriptive name as a hash. e.g. :employees;
  2. A model to map to. e.g. Employee; and
  3. A hash with :finder and :lexicon keys.

The :finder key is the XPath route to the object in the XML, and the :lexicon key is a hash with properties of the model as keys and xpath expressions as values. :lexicon will also accept a lambda as a value, which is passed the XML representation of the model and should return the value to set it to. This allows for some data manipulation, concatenation, etc. of the XML.

define_format creates a new class method on Xmlcellent::Parser named parse_format_name, with format_name being the name you passed define_format. As a convenience, there is also an Xmlcellent.parse method that loops through the the parser's defined formats and runs the first format whose :finder returns a match on the given XML.

Contributing to xmlcellent

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
  • Fork the project
  • Start a feature/bugfix branch
  • Commit and push until you are happy with your contribution
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright (c) 2011 Zach Pendleton. See LICENSE.txt for further details.