roxml 1.0 → 1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE.txt +1 -1
- data/README +2 -0
- data/lib/roxml.rb +35 -33
- data/test/fixtures/book_text_with_attribute.xml +5 -0
- data/test/mocks/mocks.rb +12 -0
- data/test/test_roxml.rb +7 -0
- metadata +6 -49
- data/doc/classes/ROXML.html +0 -352
- data/doc/classes/ROXML.src/M000003.html +0 -19
- data/doc/classes/ROXML.src/M000004.html +0 -25
- data/doc/classes/ROXML.src/M000005.html +0 -22
- data/doc/classes/ROXML/ROXML_Class.html +0 -422
- data/doc/classes/ROXML/ROXML_Class.src/M000006.html +0 -27
- data/doc/classes/ROXML/ROXML_Class.src/M000007.html +0 -18
- data/doc/classes/ROXML/ROXML_Class.src/M000008.html +0 -19
- data/doc/classes/ROXML/ROXML_Class.src/M000009.html +0 -24
- data/doc/classes/ROXML/ROXML_Class.src/M000010.html +0 -24
- data/doc/classes/ROXML/ROXML_Class.src/M000011.html +0 -18
- data/doc/classes/ROXML/ROXML_Class.src/M000012.html +0 -18
- data/doc/classes/ROXML/XMLAttributeRef.html +0 -175
- data/doc/classes/ROXML/XMLAttributeRef.src/M000015.html +0 -19
- data/doc/classes/ROXML/XMLAttributeRef.src/M000016.html +0 -19
- data/doc/classes/ROXML/XMLObjectRef.html +0 -175
- data/doc/classes/ROXML/XMLObjectRef.src/M000013.html +0 -26
- data/doc/classes/ROXML/XMLObjectRef.src/M000014.html +0 -32
- data/doc/classes/ROXML/XMLRef.html +0 -212
- data/doc/classes/ROXML/XMLRef.src/M000017.html +0 -21
- data/doc/classes/ROXML/XMLRef.src/M000018.html +0 -18
- data/doc/classes/ROXML/XMLRef.src/M000019.html +0 -18
- data/doc/classes/ROXML/XMLTextRef.html +0 -193
- data/doc/classes/ROXML/XMLTextRef.src/M000020.html +0 -26
- data/doc/classes/ROXML/XMLTextRef.src/M000021.html +0 -33
- data/doc/classes/String.html +0 -165
- data/doc/classes/String.src/M000001.html +0 -23
- data/doc/classes/String.src/M000002.html +0 -23
- data/doc/created.rid +0 -1
- data/doc/files/lib/roxml_rb.html +0 -234
- data/doc/fr_class_index.html +0 -33
- data/doc/fr_file_index.html +0 -27
- data/doc/fr_method_index.html +0 -47
- data/doc/index.html +0 -24
- data/doc/rdoc-style.css +0 -208
data/MIT-LICENSE.txt
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
The MIT License
|
2
2
|
|
3
|
-
Copyright (c) 2006 Zak Mandhro and Anders Engstrom
|
3
|
+
Copyright (c) 2006 by Zak Mandhro and Anders Engstrom
|
4
4
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6
6
|
|
data/README
ADDED
data/lib/roxml.rb
CHANGED
@@ -81,20 +81,27 @@
|
|
81
81
|
#
|
82
82
|
# For more information on available annotations, see ROXML::ROXML_Class
|
83
83
|
module ROXML
|
84
|
-
|
85
84
|
require 'rexml/document'
|
86
85
|
|
86
|
+
# Default tag behavior declaration with single
|
87
|
+
# read and write.
|
88
|
+
TAG_DEFAULT = 0
|
89
|
+
|
87
90
|
# Option that may be used to declare that
|
88
91
|
# a variable accessor should be read-only (no "accessor=(val)" is generated).
|
89
92
|
TAG_READONLY = 1
|
90
93
|
|
91
|
-
# Option that declares that an XML text element should be
|
94
|
+
# Option that declares that an XML text element's value should be
|
92
95
|
# wrapped in a CDATA section.
|
93
96
|
TAG_CDATA = 2
|
94
97
|
|
95
98
|
# Option that declares an accessor as an array (referencing "many"
|
96
99
|
# items).
|
97
100
|
TAG_ARRAY = 4
|
101
|
+
|
102
|
+
# Option that declares an xml_text annotation to define the text
|
103
|
+
# content of the container tag
|
104
|
+
TEXT_CONTENT = 5
|
98
105
|
|
99
106
|
#
|
100
107
|
# Internal base class that represents an XML - Class binding.
|
@@ -108,22 +115,6 @@ module ROXML
|
|
108
115
|
yield self if block_given?
|
109
116
|
@array = false unless @array
|
110
117
|
end
|
111
|
-
|
112
|
-
# Converts this XML reference to XML and updates the
|
113
|
-
# passed in element (XML) with data.
|
114
|
-
#
|
115
|
-
# <b>Returns</b>: The updated XML node.
|
116
|
-
def update_xml(xml, value)
|
117
|
-
xml
|
118
|
-
end
|
119
|
-
|
120
|
-
# Reads data from the XML element and populates the object
|
121
|
-
# instance accordingly.
|
122
|
-
#
|
123
|
-
# <b>Returns</b>: The updated instance.
|
124
|
-
def populate(xml, instance)
|
125
|
-
instance
|
126
|
-
end
|
127
118
|
end
|
128
119
|
|
129
120
|
# Interal class representing an XML attribute binding
|
@@ -154,14 +145,16 @@ module ROXML
|
|
154
145
|
# <element attribute="XMLAttributeRef">
|
155
146
|
# XMLTextRef
|
156
147
|
# </element>
|
157
|
-
class XMLTextRef <
|
158
|
-
attr_accessor :cdata, :wrapper
|
148
|
+
class XMLTextRef < XMLRef
|
149
|
+
attr_accessor :cdata, :wrapper, :text_content
|
159
150
|
|
160
151
|
# Updates the text in the given _xml_ block to
|
161
152
|
# the _value_ provided.
|
162
153
|
def update_xml(xml, value)
|
163
154
|
parent = (wrapper ? xml.add_element(wrapper) : xml)
|
164
|
-
if
|
155
|
+
if text_content
|
156
|
+
parent.text = (cdata ? REXML::CData.new(value.to_s.to_utf) : value.to_s.to_utf)
|
157
|
+
elsif array
|
165
158
|
value.each do |v|
|
166
159
|
parent.add_element(name).text = (cdata ? REXML::CData.new(v.to_s.to_utf) : v.to_s.to_utf)
|
167
160
|
end
|
@@ -175,10 +168,9 @@ module ROXML
|
|
175
168
|
# accordingly.
|
176
169
|
def populate(xml, instance)
|
177
170
|
data = nil
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
else
|
171
|
+
if text_content
|
172
|
+
data = xml.text
|
173
|
+
elsif array
|
182
174
|
xpath = (wrapper ? "#{wrapper}/#{name}" : "#{name}")
|
183
175
|
data = []
|
184
176
|
xml.each_element(xpath) do |e|
|
@@ -186,7 +178,9 @@ module ROXML
|
|
186
178
|
data << e.text.strip.to_latin
|
187
179
|
end
|
188
180
|
end
|
189
|
-
|
181
|
+
else
|
182
|
+
child = xml.elements[1, name]
|
183
|
+
data = child.text if child && child.text
|
190
184
|
end
|
191
185
|
instance.instance_variable_set("@#{accessor}", data) if data
|
192
186
|
instance
|
@@ -306,11 +300,17 @@ module ROXML
|
|
306
300
|
# [sym] Symbol representing the name of the accessor.
|
307
301
|
# [name] An optional name that should be used for the attribute in XML.
|
308
302
|
# Default is sym.id2name.
|
309
|
-
# [options] TAG_CDATA for character data, TAG_ARRAY for one-to-many,
|
310
|
-
#
|
303
|
+
# [options] TAG_CDATA for character data, TAG_ARRAY for one-to-many,
|
304
|
+
# TEXT_CONTENT to declare main text content for containing tag,
|
305
|
+
# and TAG_READONLY for read-only access.
|
311
306
|
# [wrapper] An optional name of a wrapping tag for this XML accessor.
|
312
307
|
#
|
313
308
|
# Example:
|
309
|
+
# class Author
|
310
|
+
# xml_attribute :role
|
311
|
+
# xml_text :text, nil, ROXML::TEXT_CONTENT
|
312
|
+
# end
|
313
|
+
#
|
314
314
|
# class Book
|
315
315
|
# xml_text :description, nil, ROXML::TAG_CDATA
|
316
316
|
# end
|
@@ -318,12 +318,14 @@ module ROXML
|
|
318
318
|
# To map:
|
319
319
|
# <book>
|
320
320
|
# <description><![CDATA[Probably the best Ruby book out there]]></description>
|
321
|
+
# <author role="primary">David Thomas</author>
|
321
322
|
# </book>
|
322
|
-
def xml_text(sym, name = nil, options =
|
323
|
+
def xml_text(sym, name = nil, options = TAG_DEFAULT, wrapper = nil)
|
323
324
|
ref = XMLTextRef.new(sym, name) do |r|
|
324
|
-
|
325
|
-
|
326
|
-
|
325
|
+
r.text_content = (TEXT_CONTENT & options==TEXT_CONTENT)
|
326
|
+
r.cdata = (TAG_CDATA & options==TAG_CDATA)
|
327
|
+
r.array = (TAG_ARRAY & options==TAG_ARRAY)
|
328
|
+
r.wrapper = wrapper if wrapper
|
327
329
|
end
|
328
330
|
add_ref(ref)
|
329
331
|
add_accessor(sym, (TAG_READONLY & options != TAG_READONLY), ref.array)
|
@@ -391,7 +393,7 @@ module ROXML
|
|
391
393
|
# If no tag name is set with xml_name method, returns default class name
|
392
394
|
# in lowercase.
|
393
395
|
def tag_name
|
394
|
-
@tag_name
|
396
|
+
@tag_name ||= self.name.split('::').last.downcase
|
395
397
|
end
|
396
398
|
|
397
399
|
# Returns array of internal reference objects, such as attributes
|
data/test/mocks/mocks.rb
CHANGED
@@ -11,6 +11,18 @@ class Book < RoxmlMockObject
|
|
11
11
|
xml_text :author
|
12
12
|
end
|
13
13
|
|
14
|
+
class Author < RoxmlMockObject
|
15
|
+
xml_attribute :role
|
16
|
+
xml_text :text, nil, ROXML::TEXT_CONTENT
|
17
|
+
end
|
18
|
+
|
19
|
+
class BookWithAuthorTextAttribute < RoxmlMockObject
|
20
|
+
xml_attribute :isbn
|
21
|
+
xml_text :title
|
22
|
+
xml_text :description, nil, ROXML::TAG_CDATA
|
23
|
+
xml_object :author, Author
|
24
|
+
end
|
25
|
+
|
14
26
|
class Contributor < RoxmlMockObject
|
15
27
|
xml_attribute :role
|
16
28
|
xml_text :name
|
data/test/test_roxml.rb
CHANGED
@@ -12,6 +12,13 @@ class TestROXML < Test::Unit::TestCase
|
|
12
12
|
assert_equal("The PickAxe", book.title)
|
13
13
|
end
|
14
14
|
|
15
|
+
# Test book with text and attribute
|
16
|
+
def test_book_author_text_attribute
|
17
|
+
book = BookWithAuthorTextAttribute.parse(fixture(:book_text_with_attribute))
|
18
|
+
assert_equal("primary",book.author.role)
|
19
|
+
assert_equal("David Thomas",book.author.text)
|
20
|
+
end
|
21
|
+
|
15
22
|
# Malformed XML parsing should throw REXML::ParseException
|
16
23
|
def test_malformed
|
17
24
|
begin
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.
|
2
|
+
rubygems_version: 0.9.0
|
3
3
|
specification_version: 1
|
4
4
|
name: roxml
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: "1.
|
7
|
-
date: 2006-
|
6
|
+
version: "1.1"
|
7
|
+
date: 2006-09-24 00:00:00 -04:00
|
8
8
|
summary: Ruby Object to XML mapping library
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -25,11 +25,13 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
|
|
25
25
|
platform: ruby
|
26
26
|
signing_key:
|
27
27
|
cert_chain:
|
28
|
+
post_install_message:
|
28
29
|
authors:
|
29
30
|
- Zak Mandhro
|
30
31
|
files:
|
31
32
|
- lib/roxml.rb
|
32
33
|
- MIT-LICENSE.txt
|
34
|
+
- README
|
33
35
|
- test/test_roxml.rb
|
34
36
|
- test/mocks
|
35
37
|
- test/fixtures
|
@@ -42,52 +44,7 @@ files:
|
|
42
44
|
- test/fixtures/book_with_contributions.xml
|
43
45
|
- test/fixtures/book_with_contributors.xml
|
44
46
|
- test/fixtures/library.xml
|
45
|
-
-
|
46
|
-
- doc/rdoc-style.css
|
47
|
-
- doc/files
|
48
|
-
- doc/classes
|
49
|
-
- doc/fr_file_index.html
|
50
|
-
- doc/fr_class_index.html
|
51
|
-
- doc/fr_method_index.html
|
52
|
-
- doc/index.html
|
53
|
-
- doc/files/lib
|
54
|
-
- doc/files/lib/roxml_rb.html
|
55
|
-
- doc/classes/String.src
|
56
|
-
- doc/classes/ROXML.src
|
57
|
-
- doc/classes/ROXML
|
58
|
-
- doc/classes/String.html
|
59
|
-
- doc/classes/ROXML.html
|
60
|
-
- doc/classes/String.src/M000001.html
|
61
|
-
- doc/classes/String.src/M000002.html
|
62
|
-
- doc/classes/ROXML.src/M000003.html
|
63
|
-
- doc/classes/ROXML.src/M000004.html
|
64
|
-
- doc/classes/ROXML.src/M000005.html
|
65
|
-
- doc/classes/ROXML/ROXML_Class.src
|
66
|
-
- doc/classes/ROXML/XMLObjectRef.src
|
67
|
-
- doc/classes/ROXML/XMLAttributeRef.src
|
68
|
-
- doc/classes/ROXML/XMLRef.src
|
69
|
-
- doc/classes/ROXML/XMLTextRef.src
|
70
|
-
- doc/classes/ROXML/ROXML_Class.html
|
71
|
-
- doc/classes/ROXML/XMLObjectRef.html
|
72
|
-
- doc/classes/ROXML/XMLAttributeRef.html
|
73
|
-
- doc/classes/ROXML/XMLRef.html
|
74
|
-
- doc/classes/ROXML/XMLTextRef.html
|
75
|
-
- doc/classes/ROXML/ROXML_Class.src/M000006.html
|
76
|
-
- doc/classes/ROXML/ROXML_Class.src/M000007.html
|
77
|
-
- doc/classes/ROXML/ROXML_Class.src/M000008.html
|
78
|
-
- doc/classes/ROXML/ROXML_Class.src/M000009.html
|
79
|
-
- doc/classes/ROXML/ROXML_Class.src/M000010.html
|
80
|
-
- doc/classes/ROXML/ROXML_Class.src/M000011.html
|
81
|
-
- doc/classes/ROXML/ROXML_Class.src/M000012.html
|
82
|
-
- doc/classes/ROXML/XMLObjectRef.src/M000013.html
|
83
|
-
- doc/classes/ROXML/XMLObjectRef.src/M000014.html
|
84
|
-
- doc/classes/ROXML/XMLAttributeRef.src/M000015.html
|
85
|
-
- doc/classes/ROXML/XMLAttributeRef.src/M000016.html
|
86
|
-
- doc/classes/ROXML/XMLRef.src/M000017.html
|
87
|
-
- doc/classes/ROXML/XMLRef.src/M000018.html
|
88
|
-
- doc/classes/ROXML/XMLRef.src/M000019.html
|
89
|
-
- doc/classes/ROXML/XMLTextRef.src/M000020.html
|
90
|
-
- doc/classes/ROXML/XMLTextRef.src/M000021.html
|
47
|
+
- test/fixtures/book_text_with_attribute.xml
|
91
48
|
test_files:
|
92
49
|
- test/test_roxml.rb
|
93
50
|
rdoc_options: []
|
data/doc/classes/ROXML.html
DELETED
@@ -1,352 +0,0 @@
|
|
1
|
-
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
-
<!DOCTYPE html
|
3
|
-
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
4
|
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
5
|
-
|
6
|
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
7
|
-
<head>
|
8
|
-
<title>Module: ROXML</title>
|
9
|
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
10
|
-
<meta http-equiv="Content-Script-Type" content="text/javascript" />
|
11
|
-
<link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
|
12
|
-
<script type="text/javascript">
|
13
|
-
// <![CDATA[
|
14
|
-
|
15
|
-
function popupCode( url ) {
|
16
|
-
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
|
17
|
-
}
|
18
|
-
|
19
|
-
function toggleCode( id ) {
|
20
|
-
if ( document.getElementById )
|
21
|
-
elem = document.getElementById( id );
|
22
|
-
else if ( document.all )
|
23
|
-
elem = eval( "document.all." + id );
|
24
|
-
else
|
25
|
-
return false;
|
26
|
-
|
27
|
-
elemStyle = elem.style;
|
28
|
-
|
29
|
-
if ( elemStyle.display != "block" ) {
|
30
|
-
elemStyle.display = "block"
|
31
|
-
} else {
|
32
|
-
elemStyle.display = "none"
|
33
|
-
}
|
34
|
-
|
35
|
-
return true;
|
36
|
-
}
|
37
|
-
|
38
|
-
// Make codeblocks hidden by default
|
39
|
-
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
|
40
|
-
|
41
|
-
// ]]>
|
42
|
-
</script>
|
43
|
-
|
44
|
-
</head>
|
45
|
-
<body>
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
<div id="classHeader">
|
50
|
-
<table class="header-table">
|
51
|
-
<tr class="top-aligned-row">
|
52
|
-
<td><strong>Module</strong></td>
|
53
|
-
<td class="class-name-in-header">ROXML</td>
|
54
|
-
</tr>
|
55
|
-
<tr class="top-aligned-row">
|
56
|
-
<td><strong>In:</strong></td>
|
57
|
-
<td>
|
58
|
-
<a href="../files/lib/roxml_rb.html">
|
59
|
-
lib/roxml.rb
|
60
|
-
</a>
|
61
|
-
<br />
|
62
|
-
</td>
|
63
|
-
</tr>
|
64
|
-
|
65
|
-
</table>
|
66
|
-
</div>
|
67
|
-
<!-- banner header -->
|
68
|
-
|
69
|
-
<div id="bodyContent">
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
<div id="contextContent">
|
74
|
-
|
75
|
-
<div id="description">
|
76
|
-
<p>
|
77
|
-
<a href="ROXML.html">ROXML</a> Ruby Object to XML mapping library. For more
|
78
|
-
information visit <a
|
79
|
-
href="http://roxml.rubyforge.org">roxml.rubyforge.org</a>
|
80
|
-
</p>
|
81
|
-
<p>
|
82
|
-
Copyright © 2004-2006 Zak Mandhro and Anders Engstrom
|
83
|
-
</p>
|
84
|
-
<p>
|
85
|
-
Permission is hereby granted, free of charge, to any person obtaining a
|
86
|
-
copy of this software and associated documentation files (the
|
87
|
-
"Software"), to deal in the Software without restriction,
|
88
|
-
including without limitation the rights to use, copy, modify, merge,
|
89
|
-
publish, distribute, sublicense, and/or sell copies of the Software, and to
|
90
|
-
permit persons to whom the Software is furnished to do so, subject to the
|
91
|
-
following conditions:
|
92
|
-
</p>
|
93
|
-
<p>
|
94
|
-
The above copyright notice and this permission notice shall be included in
|
95
|
-
all copies or substantial portions of the Software.
|
96
|
-
</p>
|
97
|
-
<p>
|
98
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
99
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
100
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
101
|
-
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
102
|
-
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
103
|
-
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
104
|
-
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
105
|
-
</p>
|
106
|
-
<h1>Quick Start Guide</h1>
|
107
|
-
<p>
|
108
|
-
This is a short usage example. See <a
|
109
|
-
href="ROXML/ROXML_Class.html">ROXML::ROXML_Class</a> and packaged test
|
110
|
-
cases for more information.
|
111
|
-
</p>
|
112
|
-
<p>
|
113
|
-
Consider an XML document representing a Library containing a number of
|
114
|
-
Books. You can map this structure to Ruby classes that provide addition
|
115
|
-
useful behavior. With <a href="ROXML.html">ROXML</a>, you can annotate the
|
116
|
-
Ruby classes as follows:
|
117
|
-
</p>
|
118
|
-
<pre>
|
119
|
-
class Book
|
120
|
-
include ROXML
|
121
|
-
|
122
|
-
xml_attribute :isbn, "ISBN"
|
123
|
-
xml_text :title
|
124
|
-
xml_text :description, nil, ROXML::TAG_CDATA
|
125
|
-
xml_text :author
|
126
|
-
end
|
127
|
-
|
128
|
-
class Library
|
129
|
-
include ROXML
|
130
|
-
|
131
|
-
xml_text :name, "NAME", ROXML::TAG_CDATA
|
132
|
-
xml_object :books, Book, ROXML::TAG_ARRAY, "books"
|
133
|
-
end
|
134
|
-
</pre>
|
135
|
-
<p>
|
136
|
-
To create a library and put a number of books in it we could run the
|
137
|
-
following code:
|
138
|
-
</p>
|
139
|
-
<pre>
|
140
|
-
book = Book.new()
|
141
|
-
book.isbn = "0201710897"
|
142
|
-
book.title = "The PickAxe"
|
143
|
-
book.description = "Best Ruby book out there!"
|
144
|
-
book.author = "David Thomas, Andrew Hunt, Dave Thomas"
|
145
|
-
|
146
|
-
lib = Library.new()
|
147
|
-
lib.name = "Favorite Books"
|
148
|
-
lib << book
|
149
|
-
</pre>
|
150
|
-
<p>
|
151
|
-
To save this information to an XML file:
|
152
|
-
</p>
|
153
|
-
<pre>
|
154
|
-
File.open("library.xml", "w") do |f|
|
155
|
-
lib.to_xml.write(f, 0)
|
156
|
-
end
|
157
|
-
</pre>
|
158
|
-
<p>
|
159
|
-
To later populate the library object from the XML file:
|
160
|
-
</p>
|
161
|
-
<pre>
|
162
|
-
lib = Library.parse(File.read("library.xml"))
|
163
|
-
</pre>
|
164
|
-
<p>
|
165
|
-
Similarly, to do a one-to-one mapping between XML objects, such as book and
|
166
|
-
publisher, you would use the <b>xml_object</b> annotation. For example:
|
167
|
-
</p>
|
168
|
-
<pre>
|
169
|
-
<book isbn="0974514055">
|
170
|
-
<title>Programming Ruby - 2nd Edition</title>
|
171
|
-
<description>Second edition of the great book.</description>
|
172
|
-
<publisher>
|
173
|
-
<name>Pragmatic Bookshelf</name>
|
174
|
-
</publisher>
|
175
|
-
</book>
|
176
|
-
</pre>
|
177
|
-
<p>
|
178
|
-
can be mapped using the following code:
|
179
|
-
</p>
|
180
|
-
<pre>
|
181
|
-
class BookWithPublisher
|
182
|
-
include ROXML
|
183
|
-
|
184
|
-
xml_name :book
|
185
|
-
xml_object :publisher, Publisher
|
186
|
-
end
|
187
|
-
</pre>
|
188
|
-
<p>
|
189
|
-
Note: In the above example, <em>xml_name</em> annotation tells <a
|
190
|
-
href="ROXML.html">ROXML</a> to set the element name to "book" for
|
191
|
-
mapping to XML. The default is XML element name is the class name in
|
192
|
-
lowercase; "bookwithpublisher" in this case.
|
193
|
-
</p>
|
194
|
-
<p>
|
195
|
-
For more information on available annotations, see <a
|
196
|
-
href="ROXML/ROXML_Class.html">ROXML::ROXML_Class</a>
|
197
|
-
</p>
|
198
|
-
|
199
|
-
</div>
|
200
|
-
|
201
|
-
|
202
|
-
</div>
|
203
|
-
|
204
|
-
<div id="method-list">
|
205
|
-
<h3 class="section-bar">Methods</h3>
|
206
|
-
|
207
|
-
<div class="name-list">
|
208
|
-
<a href="#M000003">included</a>
|
209
|
-
<a href="#M000005">method_missing</a>
|
210
|
-
<a href="#M000004">to_xml</a>
|
211
|
-
</div>
|
212
|
-
</div>
|
213
|
-
|
214
|
-
</div>
|
215
|
-
|
216
|
-
|
217
|
-
<!-- if includes -->
|
218
|
-
|
219
|
-
<div id="section">
|
220
|
-
|
221
|
-
<div id="class-list">
|
222
|
-
<h3 class="section-bar">Classes and Modules</h3>
|
223
|
-
|
224
|
-
Module <a href="ROXML/ROXML_Class.html" class="link">ROXML::ROXML_Class</a><br />
|
225
|
-
Class <a href="ROXML/XMLAttributeRef.html" class="link">ROXML::XMLAttributeRef</a><br />
|
226
|
-
Class <a href="ROXML/XMLObjectRef.html" class="link">ROXML::XMLObjectRef</a><br />
|
227
|
-
Class <a href="ROXML/XMLRef.html" class="link">ROXML::XMLRef</a><br />
|
228
|
-
Class <a href="ROXML/XMLTextRef.html" class="link">ROXML::XMLTextRef</a><br />
|
229
|
-
|
230
|
-
</div>
|
231
|
-
|
232
|
-
<div id="constants-list">
|
233
|
-
<h3 class="section-bar">Constants</h3>
|
234
|
-
|
235
|
-
<div class="name-list">
|
236
|
-
<table summary="Constants">
|
237
|
-
<tr class="top-aligned-row context-row">
|
238
|
-
<td class="context-item-name">TAG_READONLY</td>
|
239
|
-
<td>=</td>
|
240
|
-
<td class="context-item-value">1</td>
|
241
|
-
<td width="3em"> </td>
|
242
|
-
<td class="context-item-desc">
|
243
|
-
Option that may be used to declare that a variable accessor should be
|
244
|
-
read-only (no "accessor=(val)" is generated).
|
245
|
-
|
246
|
-
</td>
|
247
|
-
</tr>
|
248
|
-
<tr class="top-aligned-row context-row">
|
249
|
-
<td class="context-item-name">TAG_CDATA</td>
|
250
|
-
<td>=</td>
|
251
|
-
<td class="context-item-value">2</td>
|
252
|
-
<td width="3em"> </td>
|
253
|
-
<td class="context-item-desc">
|
254
|
-
Option that declares that an XML text element should be wrapped in a CDATA
|
255
|
-
section.
|
256
|
-
|
257
|
-
</td>
|
258
|
-
</tr>
|
259
|
-
<tr class="top-aligned-row context-row">
|
260
|
-
<td class="context-item-name">TAG_ARRAY</td>
|
261
|
-
<td>=</td>
|
262
|
-
<td class="context-item-value">4</td>
|
263
|
-
<td width="3em"> </td>
|
264
|
-
<td class="context-item-desc">
|
265
|
-
Option that declares an accessor as an array (referencing "many"
|
266
|
-
items).
|
267
|
-
|
268
|
-
</td>
|
269
|
-
</tr>
|
270
|
-
</table>
|
271
|
-
</div>
|
272
|
-
</div>
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
<!-- if method_list -->
|
280
|
-
<div id="methods">
|
281
|
-
<h3 class="section-bar">Public Class methods</h3>
|
282
|
-
|
283
|
-
<div id="method-M000003" class="method-detail">
|
284
|
-
<a name="M000003"></a>
|
285
|
-
|
286
|
-
<div class="method-heading">
|
287
|
-
<a href="ROXML.src/M000003.html" target="Code" class="method-signature"
|
288
|
-
onclick="popupCode('ROXML.src/M000003.html');return false;">
|
289
|
-
<span class="method-name">included</span><span class="method-args">(klass)</span>
|
290
|
-
</a>
|
291
|
-
</div>
|
292
|
-
|
293
|
-
<div class="method-description">
|
294
|
-
<p>
|
295
|
-
Extends the klass with the <a href="ROXML/ROXML_Class.html">ROXML_Class</a>
|
296
|
-
module methods.
|
297
|
-
</p>
|
298
|
-
</div>
|
299
|
-
</div>
|
300
|
-
|
301
|
-
<h3 class="section-bar">Public Instance methods</h3>
|
302
|
-
|
303
|
-
<div id="method-M000005" class="method-detail">
|
304
|
-
<a name="M000005"></a>
|
305
|
-
|
306
|
-
<div class="method-heading">
|
307
|
-
<a href="ROXML.src/M000005.html" target="Code" class="method-signature"
|
308
|
-
onclick="popupCode('ROXML.src/M000005.html');return false;">
|
309
|
-
<span class="method-name">method_missing</span><span class="method-args">(name, *args)</span>
|
310
|
-
</a>
|
311
|
-
</div>
|
312
|
-
|
313
|
-
<div class="method-description">
|
314
|
-
<p>
|
315
|
-
To make it easier to reference the class’s attributes all method
|
316
|
-
calls to the instance that doesn’t match an instance method are
|
317
|
-
forwarded to the class’s singleton instance. Only methods starting
|
318
|
-
with ‘tag_’ are delegated.
|
319
|
-
</p>
|
320
|
-
</div>
|
321
|
-
</div>
|
322
|
-
|
323
|
-
<div id="method-M000004" class="method-detail">
|
324
|
-
<a name="M000004"></a>
|
325
|
-
|
326
|
-
<div class="method-heading">
|
327
|
-
<a href="ROXML.src/M000004.html" target="Code" class="method-signature"
|
328
|
-
onclick="popupCode('ROXML.src/M000004.html');return false;">
|
329
|
-
<span class="method-name">to_xml</span><span class="method-args">()</span>
|
330
|
-
</a>
|
331
|
-
</div>
|
332
|
-
|
333
|
-
<div class="method-description">
|
334
|
-
<p>
|
335
|
-
Returns an REXML::Element representing this object.
|
336
|
-
</p>
|
337
|
-
</div>
|
338
|
-
</div>
|
339
|
-
|
340
|
-
|
341
|
-
</div>
|
342
|
-
|
343
|
-
|
344
|
-
</div>
|
345
|
-
|
346
|
-
|
347
|
-
<div id="validator-badges">
|
348
|
-
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
349
|
-
</div>
|
350
|
-
|
351
|
-
</body>
|
352
|
-
</html>
|