Module ROXML::ROXML_Class
In: lib/roxml.rb

This class defines the annotation methods that are mixed into your Ruby classes for XML mapping information and behavior.

See xml_name, xml_text, xml_attribute and xml_object for available annotations.

Methods

Public Instance methods

Creates a new Ruby object from XML using mapping information annotated in the class.

The input data is either a REXML::Element or a String representing the XML document.

Example

  book = Book.parse(File.read("book.xml"))

or

  book = Book.parse("<book><name>Beyond Java</name></book>")

Returns the tag name (also known as xml_name) of the class. If no tag name is set with xml_name method, returns default class name in lowercase.

Returns array of internal reference objects, such as attributes and composed XML objects

Declare an accessor for the included class that should be represented as an XML attribute.

sym
Symbol representing the name of the accessor
name
An optional name that should be used for the attribute in XML. Default is sym.id2name.
options
Valid options are TAG_READONLY to attribute as read-only

Example:

  class Book
    xml_attribute :isbn, "ISBN"
  end

To map:

  <book ISBN="0974514055"></book>

Sets the name of the XML element that represents this class. Use this to override the default lowercase class name.

Example:

  class BookWithPublisher
    xml_name :book
  end

Without the xml_name annotation, the XML mapped tag would have been "bookwithpublisher".

Declares an accessor that represents another ROXML class as child XML element (one-to-one or composition) or array of child elements (one-to-many or aggregation). Default is one-to-one. Use TAG_ARRAY option for one-to-many.

sym
Symbol representing the name of the accessor.
name
An optional name that should be used for the attribute in XML. Default is sym.id2name.
options
TAG_ARRAY for one-to-many, and TAG_READONLY for read-only access.
wrapper
An optional name of a wrapping tag for this XML accessor.

Composition example:

      <book>
        <publisher>
           <name>Pragmatic Bookshelf</name>
        </publisher>
      </book>

Can be mapped using the following code:

      class Book
          xml_object :publisher, Publisher
      end

Aggregation example:

  <library>
    <name>Ruby books</name>
    <books>
      <book/>
      <book/>
    </books>
  </library>

Can be mapped using the following code:

  class Library
      xml_text :name, nil, ROXML::TAG_CDATA
      xml_object :books, Book, ROXML::TAG_ARRAY, "books"
  end

If you don’t have the <books> tag to wrap around the list of <book> tags:

  <library>
    <name>Ruby books</name>
    <book/>
    <book/>
  </library>

You can skip the wrapper argument:

      xml_object :books, Book, ROXML::TAG_ARRAY

Declares an accessor that represents one or more XML text elements.

sym
Symbol representing the name of the accessor.
name
An optional name that should be used for the attribute in XML. Default is sym.id2name.
options
TAG_CDATA for character data, TAG_ARRAY for one-to-many, and TAG_READONLY for read-only access.
wrapper
An optional name of a wrapping tag for this XML accessor.

Example:

  class Book
    xml_text :description, nil, ROXML::TAG_CDATA
  end

To map:

  <book>
    <description><![CDATA[Probably the best Ruby book out there]]></description>
  </book>

[Validate]