Skip to content
An easy to use, extensible, well-documented library for mapping Ruby objects to XML and back.
Ruby Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
devel
doc
examples
lib/xml
test
.gitignore
LICENSE
README.md
Rakefile
TODO.txt
user_manual.in.md
user_manual_xxpath.in.md

README.md

XML-MAPPING: XML-to-object (and back) Mapper for Ruby, including XPath Interpreter

Xml-mapping is an easy to use, extensible library that allows you to semi-automatically map Ruby objects to XML trees and vice versa.

Trivial Example

sample document

<?xml version="1.0" encoding="ISO-8859-1"?>

<item reference="RF-0001">
    <Description>Stuffed Penguin</Description>
    <Quantity>10</Quantity>
    <UnitPrice>8.95</UnitPrice>
</item>

mapping class

class Item
  include XML::Mapping

  text_node :ref, "@reference"
  text_node :descr, "Description"
  numeric_node :quantity, "Quantity"
  numeric_node :unit_price, "UnitPrice"

  def total_price
    quantity*unit_price
  end
end

usage

i = Item.load_from_file("item.xml")
=> #<Item:0xb7888c90 @ref="RF-0001" @quantity=10, @descr="Stuffed Penguin", @unit_price=8.95>

i.unit_price = 42.23
xml=i.save_to_xml #convert to REXML node; there's also o.save_to_file(name)
xml.write($stdout,2)

<item reference="RF-0001">
    <Description>Stuffed Penguin</Description>
    <Quantity>10</Quantity>
    <UnitPrice>42.23</UnitPrice>
</item>

This is the most trivial example -- the mapper supports arbitrary array and hash (map) nodes, object (reference) nodes and arrays/hashes of those, polymorphic mappings, multiple mappings per class, fully programmable mappings and arbitrary user-defined node types. Read the project documentation for more information.

You can’t perform that action at this time.