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.
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>")
Declare an accessor for the included class that should be represented as an XML attribute.
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.
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.
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>