dusty-noko_parser 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.txt +229 -0
- metadata +63 -0
data/README.txt
ADDED
@@ -0,0 +1,229 @@
|
|
1
|
+
== DESCRIPTION:
|
2
|
+
|
3
|
+
Used to parse XML files with Nokogiri
|
4
|
+
|
5
|
+
|
6
|
+
== REQUIREMENTS:
|
7
|
+
|
8
|
+
nokogiri
|
9
|
+
|
10
|
+
|
11
|
+
== INSTALL:
|
12
|
+
|
13
|
+
$ gem build noko_parser.gemspec
|
14
|
+
$ sudo gem install noko_parser-x.x.x.gem
|
15
|
+
|
16
|
+
|
17
|
+
== USAGE:
|
18
|
+
|
19
|
+
See examples/monkey.rb and examples/monkeys.xml
|
20
|
+
|
21
|
+
Say you have an xml document like below. We are interested in extracting
|
22
|
+
the Monkey elements out of it and embed banana elements inside it.
|
23
|
+
|
24
|
+
<xml>
|
25
|
+
<animals>
|
26
|
+
<elephant id="3">
|
27
|
+
<name>elephant1</name>
|
28
|
+
<trunk>large</trunk>
|
29
|
+
</elephant>
|
30
|
+
<monkey id="1">
|
31
|
+
<contact>
|
32
|
+
<name type='first'>monkey1</name>
|
33
|
+
<name type='last'>monkeylastname</name>
|
34
|
+
</contact>
|
35
|
+
<bananas>
|
36
|
+
<banana>
|
37
|
+
<id>1</id>
|
38
|
+
<size>small</size>
|
39
|
+
</banana>
|
40
|
+
<banana>
|
41
|
+
<id>2</id>
|
42
|
+
<size>medium</size>
|
43
|
+
</banana>
|
44
|
+
</bananas>
|
45
|
+
<yrsold>23</yrsold>
|
46
|
+
<personality hilarious="true">quiet</personality>
|
47
|
+
<street>300 Monkey St</street>
|
48
|
+
<city>Cincinnati</city>
|
49
|
+
<state>Oh</state>
|
50
|
+
<zip>45219</zip>
|
51
|
+
</monkey>
|
52
|
+
<monkey id="2">
|
53
|
+
<contact>
|
54
|
+
<name type='first'>monkey2</name>
|
55
|
+
<name type='last'>monkeylastname</name>
|
56
|
+
</contact>
|
57
|
+
<bananas>
|
58
|
+
<banana>
|
59
|
+
<id>3</id>
|
60
|
+
<size>large</size>
|
61
|
+
</banana>
|
62
|
+
<banana>
|
63
|
+
<id>4</id>
|
64
|
+
<size>huge</size>
|
65
|
+
</banana>
|
66
|
+
</bananas>
|
67
|
+
<yrsold>33</yrsold>
|
68
|
+
<personality hilarious="false">loud</personality>
|
69
|
+
<street>301 Monkey St</street>
|
70
|
+
<city>Cincinnati</city>
|
71
|
+
<state>Oh</state>
|
72
|
+
<zip>45219</zip>
|
73
|
+
</monkey>
|
74
|
+
</animals>
|
75
|
+
</xml>
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
You first define your monkey and banana class and then describe the way to
|
80
|
+
find all the nodes you are looking for and then the properties you want. You
|
81
|
+
can also add class methods to manipulate the data.
|
82
|
+
|
83
|
+
module Fruits; end
|
84
|
+
module Animals; end
|
85
|
+
|
86
|
+
class Fruits::Banana
|
87
|
+
include NokoParser::Properties
|
88
|
+
nodes :xpath => '//banana'
|
89
|
+
|
90
|
+
property :id, :xpath => 'id'
|
91
|
+
property :size, :xpath => 'size'
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
class Animals::Monkey
|
97
|
+
include NokoParser::Properties
|
98
|
+
nodes :xpath => '//monkey'
|
99
|
+
|
100
|
+
property :id, :xpath => '.', :attribute => 'id'
|
101
|
+
|
102
|
+
property :first_name, :xpath => 'contact/name[@type="first"]'
|
103
|
+
|
104
|
+
property :last_name, :xpath => 'contact/name[@type="last"]'
|
105
|
+
|
106
|
+
property :age, :xpath => 'yrsold'
|
107
|
+
|
108
|
+
property :funny, :xpath => 'personality', :attribute => :hilarious
|
109
|
+
|
110
|
+
property :loud, :xpath => 'personality'
|
111
|
+
|
112
|
+
property :street, :xpath => 'street'
|
113
|
+
|
114
|
+
property :city, :xpath => 'city'
|
115
|
+
|
116
|
+
property :state, :xpath => 'state'
|
117
|
+
|
118
|
+
property :zip, :xpath => 'zip'
|
119
|
+
|
120
|
+
embed :bananas, :xpath => 'bananas', :class => 'Fruits::Banana'
|
121
|
+
|
122
|
+
def address
|
123
|
+
[street, city, state].compact.join(", ") + " #{zip}"
|
124
|
+
end
|
125
|
+
|
126
|
+
def age
|
127
|
+
@age.to_i
|
128
|
+
end
|
129
|
+
|
130
|
+
def funny=(value)
|
131
|
+
@funny = (value == "true" ? true : false)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
You can then call the Monkey#parse method passing in xml or the
|
137
|
+
Monkey#parse_file method passing in the path to a file.
|
138
|
+
|
139
|
+
You will receive an array of monkey objects in return.
|
140
|
+
|
141
|
+
irb(main):001:0> require 'pp'
|
142
|
+
=> true
|
143
|
+
|
144
|
+
irb(main):002:0> monkeys = Animals::Monkey.parse_file('examples/monkeys.xml')
|
145
|
+
=> #<Animals::Monkey:0x149cab8 ...>, #<Animals::Monkey:0x14992b4 ...>]
|
146
|
+
|
147
|
+
irb(main):003:0> pp monkeys.first
|
148
|
+
#<Animals::Monkey:0x149cab8
|
149
|
+
@age="23",
|
150
|
+
@bananas=
|
151
|
+
[#<Fruits::Banana:0x149a3f8 @id="1", @size="small">,
|
152
|
+
#<Fruits::Banana:0x1499b4c @id="2", @size="medium">],
|
153
|
+
@city="Cincinnati",
|
154
|
+
@first_name="monkey1",
|
155
|
+
@funny=false,
|
156
|
+
@id=1,
|
157
|
+
@last_name="monkeylastname",
|
158
|
+
@loud="quiet",
|
159
|
+
@state="Oh",
|
160
|
+
@street="300 Monkey St",
|
161
|
+
@zip="45219">
|
162
|
+
=> nil
|
163
|
+
|
164
|
+
irb(main):004:0> pp monkeys.last
|
165
|
+
#<Animals::Monkey:0x14992b4
|
166
|
+
@age="33",
|
167
|
+
@bananas=
|
168
|
+
[#<Fruits::Banana:0x1496c30 @id="3", @size="large">,
|
169
|
+
#<Fruits::Banana:0x1496384 @id="4", @size="huge">],
|
170
|
+
@city="Cincinnati",
|
171
|
+
@first_name="monkey2",
|
172
|
+
@funny=false,
|
173
|
+
@id=2,
|
174
|
+
@last_name="monkeylastname",
|
175
|
+
@loud="loud",
|
176
|
+
@state="Oh",
|
177
|
+
@street="301 Monkey St",
|
178
|
+
@zip="45219">
|
179
|
+
=> nil
|
180
|
+
|
181
|
+
irb(main):005:0> monkeys.first.age
|
182
|
+
=> 23
|
183
|
+
|
184
|
+
irb(main):006:0> monkeys.first.age.class
|
185
|
+
=> Fixnum
|
186
|
+
|
187
|
+
irb(main):007:0> monkeys.first.address
|
188
|
+
=> "300 Monkey St, Cincinnati, Oh 45219"
|
189
|
+
|
190
|
+
irb(main):008:0> monkeys.first.funny
|
191
|
+
=> false
|
192
|
+
|
193
|
+
irb(main):010:0> pp monkeys.first.bananas
|
194
|
+
[#<Fruits::Banana:0x149a3f8 @id="1", @size="small">,
|
195
|
+
#<Fruits::Banana:0x1499b4c @id="2", @size="medium">]
|
196
|
+
=> nil
|
197
|
+
|
198
|
+
irb(main):004:0> pp monkeys.last.bananas
|
199
|
+
[#<Fruits::Banana:0x1496c30 @id="3", @size="large">,
|
200
|
+
#<Fruits::Banana:0x1496384 @id="4", @size="huge">]
|
201
|
+
=> nil
|
202
|
+
|
203
|
+
== FEATURES/PROBLEMS:
|
204
|
+
|
205
|
+
|
206
|
+
== LICENSE:
|
207
|
+
|
208
|
+
(The MIT License)
|
209
|
+
|
210
|
+
Copyright (c) 2008 FIX
|
211
|
+
|
212
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
213
|
+
a copy of this software and associated documentation files (the
|
214
|
+
'Software'), to deal in the Software without restriction, including
|
215
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
216
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
217
|
+
permit persons to whom the Software is furnished to do so, subject to
|
218
|
+
the following conditions:
|
219
|
+
|
220
|
+
The above copyright notice and this permission notice shall be
|
221
|
+
included in all copies or substantial portions of the Software.
|
222
|
+
|
223
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
224
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
225
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
226
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
227
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
228
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
229
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dusty-noko_parser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.8
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dusty Doris
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-16 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: nokogiri
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description:
|
26
|
+
email: github@dusty.name
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.txt
|
33
|
+
files: []
|
34
|
+
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://code.dusty.name
|
37
|
+
licenses:
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project: none
|
58
|
+
rubygems_version: 1.3.5
|
59
|
+
signing_key:
|
60
|
+
specification_version: 2
|
61
|
+
summary: Wrapper around Nokogiri to easily parse xml files with xpath
|
62
|
+
test_files: []
|
63
|
+
|