/
README.txt
229 lines (173 loc) · 5.49 KB
/
README.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
== DESCRIPTION:
Used to parse XML files with Nokogiri
== REQUIREMENTS:
nokogiri
== INSTALL:
$ gem build noko_parser.gemspec
$ sudo gem install noko_parser-x.x.x.gem
== USAGE:
See examples/monkey.rb and examples/monkeys.xml
Say you have an xml document like below. We are interested in extracting
the Monkey elements out of it and embed banana elements inside it.
<xml>
<animals>
<elephant id="3">
<name>elephant1</name>
<trunk>large</trunk>
</elephant>
<monkey id="1">
<contact>
<name type='first'>monkey1</name>
<name type='last'>monkeylastname</name>
</contact>
<bananas>
<banana>
<id>1</id>
<size>small</size>
</banana>
<banana>
<id>2</id>
<size>medium</size>
</banana>
</bananas>
<yrsold>23</yrsold>
<personality hilarious="true">quiet</personality>
<street>300 Monkey St</street>
<city>Cincinnati</city>
<state>Oh</state>
<zip>45219</zip>
</monkey>
<monkey id="2">
<contact>
<name type='first'>monkey2</name>
<name type='last'>monkeylastname</name>
</contact>
<bananas>
<banana>
<id>3</id>
<size>large</size>
</banana>
<banana>
<id>4</id>
<size>huge</size>
</banana>
</bananas>
<yrsold>33</yrsold>
<personality hilarious="false">loud</personality>
<street>301 Monkey St</street>
<city>Cincinnati</city>
<state>Oh</state>
<zip>45219</zip>
</monkey>
</animals>
</xml>
You first define your monkey and banana class and then describe the way to
find all the nodes you are looking for and then the properties you want. You
can also add class methods to manipulate the data.
module Fruits; end
module Animals; end
class Fruits::Banana
include NokoParser::Properties
nodes :xpath => '//banana'
property :id, :xpath => 'id'
property :size, :xpath => 'size'
end
class Animals::Monkey
include NokoParser::Properties
nodes :xpath => '//monkey'
property :id, :xpath => '.', :attribute => 'id'
property :first_name, :xpath => 'contact/name[@type="first"]'
property :last_name, :xpath => 'contact/name[@type="last"]'
property :age, :xpath => 'yrsold'
property :funny, :xpath => 'personality', :attribute => :hilarious
property :loud, :xpath => 'personality'
property :street, :xpath => 'street'
property :city, :xpath => 'city'
property :state, :xpath => 'state'
property :zip, :xpath => 'zip'
embed :bananas, :xpath => 'bananas', :class => 'Fruits::Banana'
def address
[street, city, state].compact.join(", ") + " #{zip}"
end
def age
@age.to_i
end
def funny=(value)
@funny = (value == "true" ? true : false)
end
end
You can then call the Monkey#parse method passing in xml or the
Monkey#parse_file method passing in the path to a file.
You will receive an array of monkey objects in return.
irb(main):001:0> require 'pp'
=> true
irb(main):002:0> monkeys = Animals::Monkey.parse_file('examples/monkeys.xml')
=> #<Animals::Monkey:0x149cab8 ...>, #<Animals::Monkey:0x14992b4 ...>]
irb(main):003:0> pp monkeys.first
#<Animals::Monkey:0x149cab8
@age="23",
@bananas=
[#<Fruits::Banana:0x149a3f8 @id="1", @size="small">,
#<Fruits::Banana:0x1499b4c @id="2", @size="medium">],
@city="Cincinnati",
@first_name="monkey1",
@funny=false,
@id=1,
@last_name="monkeylastname",
@loud="quiet",
@state="Oh",
@street="300 Monkey St",
@zip="45219">
=> nil
irb(main):004:0> pp monkeys.last
#<Animals::Monkey:0x14992b4
@age="33",
@bananas=
[#<Fruits::Banana:0x1496c30 @id="3", @size="large">,
#<Fruits::Banana:0x1496384 @id="4", @size="huge">],
@city="Cincinnati",
@first_name="monkey2",
@funny=false,
@id=2,
@last_name="monkeylastname",
@loud="loud",
@state="Oh",
@street="301 Monkey St",
@zip="45219">
=> nil
irb(main):005:0> monkeys.first.age
=> 23
irb(main):006:0> monkeys.first.age.class
=> Fixnum
irb(main):007:0> monkeys.first.address
=> "300 Monkey St, Cincinnati, Oh 45219"
irb(main):008:0> monkeys.first.funny
=> false
irb(main):010:0> pp monkeys.first.bananas
[#<Fruits::Banana:0x149a3f8 @id="1", @size="small">,
#<Fruits::Banana:0x1499b4c @id="2", @size="medium">]
=> nil
irb(main):004:0> pp monkeys.last.bananas
[#<Fruits::Banana:0x1496c30 @id="3", @size="large">,
#<Fruits::Banana:0x1496384 @id="4", @size="huge">]
=> nil
== FEATURES/PROBLEMS:
== LICENSE:
(The MIT License)
Copyright (c) 2008 FIX
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:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.