github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

Empact / roxml

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 79
    • 11
  • Source
  • Commits
  • Network (11)
  • Issues (7)
  • Downloads (13)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (2)
    • gh-pages
    • master ✓
  • Tags (13)
    • v2.5.1
    • v2.5.0
    • v2.4.3
    • v2.4.2
    • v2.4.1
    • v2.4.0
    • v2.3.2
    • v2.3.1
    • v2.3.0
    • v2.2
    • v2.1
    • v2.0
    • initial_import
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

ROXML is a module for binding Ruby classes to XML. It supports custom mapping and bidirectional marshalling between Ruby and XML using annotation-style class methods, via Nokogiri or LibXML. — Read more

  cancel

http://roxml.rubyforge.org/

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Release 3.1.5 
Empact (author)
Thu Dec 17 22:25:02 -0800 2009
commit  0521b278bb2964def15470872b79ddae5b3345a3
tree    b5162de6c4a9eb36be96dd1887b7cceef4fc0490
parent  a436035fc435734f2e5b86483a19d60866e1f1bc
roxml /
name age
history
message
file .gitignore Fri Oct 16 17:07:25 -0700 2009 Ignore rdoc folder [Empact]
file .gitmodules Thu Jan 15 02:12:50 -0800 2009 Use hoe-style Rakefile-generated gemspec & such [Empact]
file History.txt Thu Dec 17 22:12:30 -0800 2009 Don't use tasks/ because it is picked up by ven... [Empact]
file LICENSE Sun Oct 18 09:26:49 -0700 2009 Pick up a few minor adjustments from the jewele... [Empact]
file README.rdoc Mon Oct 26 22:26:31 -0700 2009 Don't output crazy, context-less namespaces and... [Empact]
file Rakefile Thu Dec 17 22:12:30 -0800 2009 Don't use tasks/ because it is picked up by ven... [Empact]
file TODO Mon Oct 26 22:26:31 -0700 2009 Don't output crazy, context-less namespaces and... [Empact]
file VERSION Thu Dec 17 22:25:02 -0800 2009 Release 3.1.5 [Empact]
directory examples/ Thu Dec 17 21:52:40 -0800 2009 Require the non-deprecated active_record [Empact]
directory lib/ Thu Dec 17 22:25:02 -0800 2009 Release 3.1.5 [Empact]
file roxml.gemspec Thu Dec 17 22:25:02 -0800 2009 Release 3.1.5 [Empact]
directory spec/ Thu Dec 17 22:24:04 -0800 2009 Test non-latin characters are handled properly ... [Empact]
directory test/ Mon Oct 26 17:22:38 -0700 2009 Rename Definition#type to #sought_type [Empact]
directory website/ Wed Oct 14 16:49:14 -0700 2009 Remove REXML support altogether. Now we *reall... [Empact]
README.rdoc

ROXML Ruby Object to XML mapping library.

For more information visit:

    http://roxml.rubyforge.org/rdoc/
    http://empact.github.com/roxml/
    http://rubyforge.org/projects/roxml/

Please submit bugs here:

    http://github.com/Empact/roxml/issues

Quick Start Guide

This is a short usage example. See ROXML::ClassMethods::Declarations and packaged test cases for more information.

Basic Mapping

Consider an XML document representing a Library containing a number of Books. You can map this structure to Ruby classes that provide addition useful behavior. With ROXML, you can annotate the Ruby classes as follows:

  class Book
    include ROXML

    xml_accessor :isbn, :from => "@ISBN" # attribute with name 'ISBN'
    xml_accessor :title
    xml_accessor :description, :cdata => true  # text node with cdata protection
    xml_accessor :author
  end

  class Library
    include ROXML

    xml_accessor :name, :from => "NAME", :cdata => true
    xml_accessor :books, :as => [Book] # by default roxml searches for books for in <book> child nodes, then, if none are present, in ./books/book children
  end

To create a library and put a number of books in it we could run the following code:

  book = Book.new
  book.isbn = "0201710897"
  book.title = "The PickAxe"
  book.description = "Best Ruby book out there!"
  book.author = "David Thomas, Andrew Hunt, Dave Thomas"

  lib = Library.new
  lib.name = "Favorite Books"
  lib.books = [book]

To save this information to an XML file:

  doc = ROXML::XML::Document.new
  doc.root = lib.to_xml
  doc.save("library.xml")

To later populate the library object from the XML file:

  lib = Library.from_xml(File.read("library.xml"))

Similarly, to do a one-to-one mapping between XML objects, such as book and publisher, you would add a reference to another ROXML class. For example:

  <book isbn="0974514055">
    <title>Programming Ruby - 2nd Edition</title>
    <description>Second edition of the great book.</description>
    <publisher>
      <name>Pragmatic Bookshelf</name>
    </publisher>
  </book>

can be mapped using the following code:

  class Publisher
    include ROXML

    xml_accessor :name

    # other important functionality
  end

  class BookWithPublisher
    include ROXML

    xml_name 'book'
    xml_reader :publisher, :as => Publisher

    #  or, alternatively, if no class is needed to hang functionality on:
    # xml_reader :publisher, :from => 'name', :in => 'publisher'
  end

Note: In the above example, xml_name annotation tells ROXML to set the element name to "book" for mapping to XML. The default is XML element name is the class name in lowercase; "bookwithpublisher" in this case.

Namespace Support

Namespaced nodes are supported via the xml_namespace and xml_namespaces declarations and the :from and :namespace attr options. See spec/xml/namespace_spec.rb for usage.

Note that ROXML does not currently support outputting namespaced nodes. This is planned for a future version.

Manipulation

Extending the above examples, say you want to parse a book’s page count and have it available as an Integer. In such a case, you can extend any object with a block to manipulate it’s value at parse time. For example:

  class Dog
    include ROXML

    xml_reader(:age, :from => '@human_years', :as => Integer) {|years| years * 7 }
  end

The result of the block above is stored, rather than the actual value parsed from the document.

Construction

Object life-cycle is as follows: .from_xml is called with a first argument representing the xml in file, string, or path form, and with optional initialization_args following.

Firt .new and thus #initialize, is called with those same initialization_args, or no args if none are present. Then the object is populated with the attribute values from xml. Then the #after_parse callback is called, with no arguments.

In #after_parse you can ensure that your object initialization is complete, including initialization which requires more than one variable in concert.

E.g.:

  class Measurement
    include ROXML

    xml_reader :units, :from => :attr
    xml_reader :value, :from => :content

    def initialize(value = 0, units = 'meters')
      to_metric
    end

  private
    def after_parse
      # xml attributes of self are already valid
      to_metric
    end

    def to_metric
      # translate units & value into metric, for example
    end
  end

One important use of this approach is to make ROXML object which may or may not include an xml backing, which may be used via new construction as well as from_xml construction.

Selecting a parser

By default, ROXML will use Nokogiri if it is available, followed by LibXML. If you’d like to explicitly require one or the other, you may do the following:

  module ROXML
    XML_PARSER = 'nokogiri' # or 'libxml'
  end
  require 'roxml'

For more information on available annotations, see ROXML::ClassMethods::Declarations

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add specs for it. This is important so I don’t break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but
     bump version in a commit by itself I can ignore when I pull)
    
  • Send me a pull request. Bonus points for topic branches.

Copyright

Copyright © 2004-2009 Ben Woosley, Zak Mandhro and Anders Engstrom. See LICENSE for details.

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server