rexml 3.2.4 → 3.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,97 @@
1
+ == Module Node
2
+
3
+ :include: ../tocs/node_toc.rdoc
4
+
5
+ === Siblings
6
+
7
+ ==== Task: Find Previous Sibling
8
+
9
+ Use method
10
+ {Node.previous_sibling_node}[../../../../REXML/Node.html#method-i-previous_sibling]
11
+ to retrieve the previous sibling:
12
+
13
+ d = REXML::Document.new('<root><a/><b/><c/></root>')
14
+ b = d.root[1] # => <b/>
15
+ b.previous_sibling_node # => <a/>
16
+
17
+ ==== Task: Find Next Sibling
18
+
19
+ Use method
20
+ {Node.next_sibling_node}[../../../../REXML/Node.html#method-i-next_sibling]
21
+ to retrieve the next sibling:
22
+
23
+ d = REXML::Document.new('<root><a/><b/><c/></root>')
24
+ b = d.root[1] # => <b/>
25
+ b.next_sibling_node # => <c/>
26
+
27
+ === Position
28
+
29
+ ==== Task: Find Own Index Among Siblings
30
+
31
+ Use method
32
+ {Node.index_in_parent}[../../../../REXML/Node.html#method-i-index_in_parent]
33
+ to retrieve the 1-based index of this node among its siblings:
34
+
35
+ d = REXML::Document.new('<root><a/><b/><c/></root>')
36
+ b = d.root[1] # => <b/>
37
+ b.index_in_parent # => 2
38
+
39
+ === Recursive Traversal
40
+
41
+ ==== Task: Traverse Each Recursively
42
+
43
+ Use method
44
+ {Node.each_recursive}[../../../../REXML/Node.html#method-i-each_recursive]
45
+ to traverse a tree of nodes recursively:
46
+
47
+ xml_string = '<root><a><b><c></c></b><b><c></c></b></a></root>'
48
+ d = REXML::Document.new(xml_string)
49
+ d.root.each_recursive {|node| p node }
50
+
51
+ Output:
52
+
53
+ <a> ... </>
54
+ <b> ... </>
55
+ <c/>
56
+ <b> ... </>
57
+ <c/>
58
+
59
+ === Recursive Search
60
+
61
+ ==== Task: Traverse Each Recursively
62
+
63
+ Use method
64
+ {Node.find_first_recursive}[../../../../REXML/Node.html#method-i-find_first_recursive]
65
+ to search a tree of nodes recursively:
66
+
67
+ xml_string = '<root><a><b><c></c></b><b><c></c></b></a></root>'
68
+ d = REXML::Document.new(xml_string)
69
+ d.root.find_first_recursive {|node| node.name == 'c' } # => <c/>
70
+
71
+ === Representation
72
+
73
+ ==== Task: Represent a String
74
+
75
+ Use method {Node.to_s}[../../../../REXML/Node.html#method-i-to_s]
76
+ to represent the node as a string:
77
+
78
+ xml_string = '<root><a><b><c></c></b><b><c></c></b></a></root>'
79
+ d = REXML::Document.new(xml_string)
80
+ d.root.to_s # => "<root><a><b><c/></b><b><c/></b></a></root>"
81
+
82
+ === Parent?
83
+
84
+ ==== Task: Determine Whether the Node is a Parent
85
+
86
+ Use method {Node.parent?}[../../../../REXML/Node.html#method-i-parent-3F]
87
+ to determine whether the node is a parent;
88
+ class Text derives from Node:
89
+
90
+ d = REXML::Document.new('<root><a/>text<b/>more<c/></root>')
91
+ t = d.root[1] # => "text"
92
+ t.parent? # => false
93
+
94
+ Class Parent also derives from Node, but overrides this method:
95
+
96
+ p = REXML::Parent.new
97
+ p.parent? # => true
@@ -0,0 +1,267 @@
1
+ == Class Parent
2
+
3
+ Class Parent has methods from its superclasses and included modules;
4
+ see:
5
+
6
+ - {Tasks for Child}[child_rdoc.html].
7
+ - {Tasks for Node}[node_rdoc.html].
8
+ - {Module Enumerable}[https://docs.ruby-lang.org/en/master/Enumerable.html].
9
+
10
+ :include: ../tocs/parent_toc.rdoc
11
+
12
+ === Queries
13
+
14
+ ==== Task: Get the Count of Children
15
+
16
+ Use method {Parent#size}[../../../../REXML/Parent.html#method-i-size]
17
+ (or its alias +length+) to get the count of the parent's children:
18
+
19
+ p = REXML::Parent.new
20
+ p.size # => 0
21
+ xml_string = '<root><a/><b/><c/></root>'
22
+ d = REXML::Document.new(xml_string)
23
+ d.root.size # => 3
24
+
25
+ ==== Task: Get the Child at a Given Index
26
+
27
+ Use method {Parent#[]}[../../../../REXML/Parent.html#method-i-5B-5D]
28
+ to get the child at a given index:
29
+
30
+ xml_string = '<root><a/><b/><c/></root>'
31
+ d = REXML::Document.new(xml_string)
32
+ d.root[1] # => <b/>
33
+ d.root[-1] # => <c/>
34
+ d.root[50] # => nil
35
+
36
+ ==== Task: Get the Index of a Given Child
37
+
38
+ Use method {Parent#index}[../../../../REXML/Parent.html#method-i-index]
39
+ to get the index (0-based offset) of a child:
40
+
41
+ d = REXML::Document.new('<root></root>')
42
+ root = d.root
43
+ e0 = REXML::Element.new('foo')
44
+ e1 = REXML::Element.new('bar')
45
+ root.add(e0) # => <foo/>
46
+ root.add(e1) # => <bar/>
47
+ root.add(e0) # => <foo/>
48
+ root.add(e1) # => <bar/>
49
+ root.index(e0) # => 0
50
+ root.index(e1) # => 1
51
+
52
+ ==== Task: Get the Children
53
+
54
+ Use method {Parent#children}[../../../../REXML/Parent.html#method-i-children]
55
+ (or its alias +to_a+) to get the parent's children:
56
+
57
+ xml_string = '<root><a/><b/><c/></root>'
58
+ d = REXML::Document.new(xml_string)
59
+ d.root.children # => [<a/>, <b/>, <c/>]
60
+
61
+ ==== Task: Determine Whether the Node is a Parent
62
+
63
+ Use method {Parent#parent?}[../../../../REXML/Parent.html#method-i-parent-3F]
64
+ to determine whether the node is a parent;
65
+ class Text derives from Node:
66
+
67
+ d = REXML::Document.new('<root><a/>text<b/>more<c/></root>')
68
+ t = d.root[1] # => "text"
69
+ t.parent? # => false
70
+
71
+ Class Parent also derives from Node, but overrides this method:
72
+
73
+ p = REXML::Parent.new
74
+ p.parent? # => true
75
+
76
+ === Additions
77
+
78
+ ==== Task: Add a Child at the Beginning
79
+
80
+ Use method {Parent#unshift}[../../../../REXML/Parent.html#method-i-unshift]
81
+ to add a child as at the beginning of the children:
82
+
83
+ xml_string = '<root><a/><b/><c/></root>'
84
+ d = REXML::Document.new(xml_string)
85
+ d.root.children # => [<a/>, <b/>, <c/>]
86
+ d.root.unshift REXML::Element.new('d')
87
+ d.root.children # => [<d/>, <a/>, <b/>, <c/>]
88
+
89
+ ==== Task: Add a Child at the End
90
+
91
+ Use method {Parent#<<}[../../../../REXML/Parent.html#method-i-3C-3C]
92
+ (or an alias +push+ or +add+) to add a child as at the end of the children:
93
+
94
+ xml_string = '<root><a/><b/><c/></root>'
95
+ d = REXML::Document.new(xml_string)
96
+ d.root.children # => [<a/>, <b/>, <c/>]
97
+ d.root << REXML::Element.new('d')
98
+ d.root.children # => [<a/>, <b/>, <c/>, <d/>]
99
+
100
+ ==== Task: Replace a Child with Another Child
101
+
102
+ Use method {Parent#replace}[../../../../REXML/Parent.html#method-i-replace]
103
+
104
+ xml_string = '<root><a/><b/><c/></root>'
105
+ d = REXML::Document.new(xml_string)
106
+ d.root.children # => [<a/>, <b/>, <c/>]
107
+ b = d.root[1] # => <b/>
108
+ d.replace_child(b, REXML::Element.new('d'))
109
+ d.root.children # => [<a/>, <c/>]
110
+
111
+ ==== Task: Replace Multiple Children with Another Child
112
+
113
+ Use method {Parent#[]=}[../../../../REXML/Parent.html#method-i-parent-5B-5D-3D]
114
+ to replace multiple consecutive children with another child:
115
+
116
+ xml_string = '<root><a/><b/><c/><d/></root>'
117
+ d = REXML::Document.new(xml_string)
118
+ d.root.children # => [<a/>, <b/>, <c/>, <d/>]
119
+ d.root[1, 2] = REXML::Element.new('x')
120
+ d.root.children # => [<a/>, <x/>, <d/>]
121
+ d.root[1, 5] = REXML::Element.new('x')
122
+ d.root.children # => [<a/>, <x/>] # BUG?
123
+
124
+ ==== Task: Insert Child Before a Given Child
125
+
126
+ Use method {Parent#insert_before}[../../../../REXML/Parent.html#method-i-insert_before]
127
+ to insert a child immediately before a given child:
128
+
129
+ xml_string = '<root><a/><b/><c/></root>'
130
+ d = REXML::Document.new(xml_string)
131
+ d.root.children # => [<a/>, <b/>, <c/>]
132
+ b = d.root[1] # => <b/>
133
+ x = REXML::Element.new('x')
134
+ d.root.insert_before(b, x)
135
+ d.root.children # => [<a/>, <x/>, <b/>, <c/>]
136
+
137
+ ==== Task: Insert Child After a Given Child
138
+
139
+ Use method {Parent#insert_after}[../../../../REXML/Parent.html#method-i-insert_after]
140
+ to insert a child immediately after a given child:
141
+
142
+ xml_string = '<root><a/><b/><c/></root>'
143
+ d = REXML::Document.new(xml_string)
144
+ d.root.children # => [<a/>, <b/>, <c/>]
145
+ b = d.root[1] # => <b/>
146
+ x = REXML::Element.new('x')
147
+ d.root.insert_after(b, x)
148
+ d.root.children # => [<a/>, <b/>, <x/>, <c/>]
149
+
150
+ === Deletions
151
+
152
+ ==== Task: Remove a Given Child
153
+
154
+ Use method {Parent#delete}[../../../../REXML/Parent.html#method-i-delete]
155
+ to remove all occurrences of a given child:
156
+
157
+ d = REXML::Document.new('<root></root>')
158
+ a = REXML::Element.new('a')
159
+ b = REXML::Element.new('b')
160
+ d.root.add(a)
161
+ d.root.add(b)
162
+ d.root.add(a)
163
+ d.root.add(b)
164
+ d.root.children # => [<a/>, <b/>, <a/>, <b/>]
165
+ d.root.delete(b)
166
+ d.root.children # => [<a/>, <a/>]
167
+
168
+ ==== Task: Remove the Child at a Specified Offset
169
+
170
+ Use method {Parent#delete_at}[../../../../REXML/Parent.html#method-i-delete_at]
171
+ to remove the child at a specified offset:
172
+
173
+ d = REXML::Document.new('<root></root>')
174
+ a = REXML::Element.new('a')
175
+ b = REXML::Element.new('b')
176
+ d.root.add(a)
177
+ d.root.add(b)
178
+ d.root.add(a)
179
+ d.root.add(b)
180
+ d.root.children # => [<a/>, <b/>, <a/>, <b/>]
181
+ d.root.delete_at(2)
182
+ d.root.children # => [<a/>, <b/>, <b/>]
183
+
184
+ ==== Task: Remove Children That Meet Specified Criteria
185
+
186
+ Use method {Parent#delete_if}[../../../../REXML/Parent.html#method-i-delete_if]
187
+ to remove children that meet criteria specified in the given block:
188
+
189
+ d = REXML::Document.new('<root></root>')
190
+ d.root.add(REXML::Element.new('x'))
191
+ d.root.add(REXML::Element.new('xx'))
192
+ d.root.add(REXML::Element.new('xxx'))
193
+ d.root.add(REXML::Element.new('xxxx'))
194
+ d.root.children # => [<x/>, <xx/>, <xxx/>, <xxxx/>]
195
+ d.root.delete_if {|child| child.name.size.odd? }
196
+ d.root.children # => [<xx/>, <xxxx/>]
197
+
198
+ === Iterations
199
+
200
+ ==== Task: Iterate Over Children
201
+
202
+ Use method {Parent#each_child}[../../../../REXML/Parent.html#method-i-each_child]
203
+ (or its alias +each+) to iterate over all children:
204
+
205
+ xml_string = '<root><a/><b/><c/></root>'
206
+ d = REXML::Document.new(xml_string)
207
+ d.root.children # => [<a/>, <b/>, <c/>]
208
+ d.root.each_child {|child| p child }
209
+
210
+ Output:
211
+
212
+ <a/>
213
+ <b/>
214
+ <c/>
215
+
216
+ ==== Task: Iterate Over Child Indexes
217
+
218
+ Use method {Parent#each_index}[../../../../REXML/Parent.html#method-i-each_index]
219
+ to iterate over all child indexes:
220
+
221
+ xml_string = '<root><a/><b/><c/></root>'
222
+ d = REXML::Document.new(xml_string)
223
+ d.root.children # => [<a/>, <b/>, <c/>]
224
+ d.root.each_index {|child| p child }
225
+
226
+ Output:
227
+
228
+ 0
229
+ 1
230
+ 2
231
+
232
+ === Clones
233
+
234
+ ==== Task: Clone Deeply
235
+
236
+ Use method {Parent#deep_clone}[../../../../REXML/Parent.html#method-i-deep_clone]
237
+ to clone deeply; that is, to clone every nested node that is a Parent object:
238
+
239
+ xml_string = <<-EOT
240
+ <?xml version="1.0" encoding="UTF-8"?>
241
+ <bookstore>
242
+ <book category="cooking">
243
+ <title lang="en">Everyday Italian</title>
244
+ <author>Giada De Laurentiis</author>
245
+ <year>2005</year>
246
+ <price>30.00</price>
247
+ </book>
248
+ <book category="children">
249
+ <title lang="en">Harry Potter</title>
250
+ <author>J K. Rowling</author>
251
+ <year>2005</year>
252
+ <price>29.99</price>
253
+ </book>
254
+ <book category="web">
255
+ <title lang="en">Learning XML</title>
256
+ <author>Erik T. Ray</author>
257
+ <year>2003</year>
258
+ <price>39.95</price>
259
+ </book>
260
+ </bookstore>
261
+ EOT
262
+ d = REXML::Document.new(xml_string)
263
+ root = d.root
264
+ shallow = root.clone
265
+ deep = root.deep_clone
266
+ shallow.to_s.size # => 12
267
+ deep.to_s.size # => 590
@@ -0,0 +1,12 @@
1
+ Tasks on this page:
2
+
3
+ - {Relationships}[#label-Relationships]
4
+ - {Task: Set the Parent}[#label-Task-3A+Set+the+Parent]
5
+ - {Task: Insert Previous Sibling}[#label-Task-3A+Insert+Previous+Sibling]
6
+ - {Task: Insert Next Sibling}[#label-Task-3A+Insert+Next+Sibling]
7
+ - {Removal or Replacement}[#label-Removal+or+Replacement]
8
+ - {Task: Remove Child from Parent}[#label-Task-3A+Remove+Child+from+Parent]
9
+ - {Task: Replace Child}[#label-Task-3A+Replace+Child]
10
+ - {Document}[#label-Document]
11
+ - {Task: Get the Document}[#label-Task-3A+Get+the+Document]
12
+
@@ -0,0 +1,30 @@
1
+ Tasks on this page:
2
+
3
+ - {New Document}[#label-New+Document]
4
+ - {Task: Create an Empty Document}[#label-Task-3A+Create+an+Empty+Document]
5
+ - {Task: Parse a String into a New Document}[#label-Task-3A+Parse+a+String+into+a+New+Document]
6
+ - {Task: Parse an IO Stream into a New Document}[#label-Task-3A+Parse+an+IO+Stream+into+a+New+Document]
7
+ - {Task: Create a Document from an Existing Document}[#label-Task-3A+Create+a+Document+from+an+Existing+Document]
8
+ - {Task: Clone a Document}[#label-Task-3A+Clone+a+Document]
9
+ - {Document Type}[#label-Document+Type]
10
+ - {Task: Get the Document Type}[#label-Task-3A+Get+the+Document+Type]
11
+ - {Task: Set the Document Type}[#label-Task-3A+Set+the+Document+Type]
12
+ - {XML Declaration}[#label-XML+Declaration]
13
+ - {Task: Get the XML Declaration}[#label-Task-3A+Get+the+XML+Declaration]
14
+ - {Task: Set the XML Declaration}[#label-Task-3A+Set+the+XML+Declaration]
15
+ - {Children}[#label-Children]
16
+ - {Task: Add an Element Child}[#label-Task-3A+Add+an+Element+Child]
17
+ - {Task: Add a Non-Element Child}[#label-Task-3A+Add+a+Non-Element+Child]
18
+ - {Writing}[#label-Writing]
19
+ - {Task: Write to $stdout}[#label-Task-3A+Write+to+-24stdout]
20
+ - {Task: Write to IO Stream}[#label-Task-3A+Write+to+IO+Stream]
21
+ - {Task: Write with No Indentation}[#label-Task-3A+Write+with+No+Indentation]
22
+ - {Task: Write with Specified Indentation}[#label-Task-3A+Write+with+Specified+Indentation]
23
+ - {Querying}[#label-Querying]
24
+ - {Task: Get the Document}[#label-Task-3A+Get+the+Document]
25
+ - {Task: Get the Encoding}[#label-Task-3A+Get+the+Encoding]
26
+ - {Task: Get the Node Type}[#label-Task-3A+Get+the+Node+Type]
27
+ - {Task: Get the Root Element}[#label-Task-3A+Get+the+Root+Element]
28
+ - {Task: Determine Whether Stand-Alone}[#label-Task-3A+Determine+Whether+Stand-Alone]
29
+ - {Task: Get the Version}[#label-Task-3A+Get+the+Version]
30
+
@@ -0,0 +1,55 @@
1
+ Tasks on this page:
2
+
3
+ - {New Element}[#label-New+Element]
4
+ - {Task: Create a Default Element}[#label-Task-3A+Create+a+Default+Element]
5
+ - {Task: Create a Named Element}[#label-Task-3A+Create+a+Named+Element]
6
+ - {Task: Create an Element with Name and Parent}[#label-Task-3A+Create+an+Element+with+Name+and+Parent]
7
+ - {Task: Create an Element with Name, Parent, and Context}[#label-Task-3A+Create+an+Element+with+Name-2C+Parent-2C+and+Context]
8
+ - {Task: Create a Shallow Clone}[#label-Task-3A+Create+a+Shallow+Clone]
9
+ - {Attributes}[#label-Attributes]
10
+ - {Task: Create and Add an Attribute}[#label-Task-3A+Create+and+Add+an+Attribute]
11
+ - {Task: Add an Existing Attribute}[#label-Task-3A+Add+an+Existing+Attribute]
12
+ - {Task: Add Multiple Attributes from a Hash}[#label-Task-3A+Add+Multiple+Attributes+from+a+Hash]
13
+ - {Task: Add Multiple Attributes from an Array}[#label-Task-3A+Add+Multiple+Attributes+from+an+Array]
14
+ - {Task: Retrieve the Value for an Attribute Name}[#label-Task-3A+Retrieve+the+Value+for+an+Attribute+Name]
15
+ - {Task: Retrieve the Attribute Value for a Name and Namespace}[#label-Task-3A+Retrieve+the+Attribute+Value+for+a+Name+and+Namespace]
16
+ - {Task: Delete an Attribute}[#label-Task-3A+Delete+an+Attribute]
17
+ - {Task: Determine Whether the Element Has Attributes}[#label-Task-3A+Determine+Whether+the+Element+Has+Attributes]
18
+ - {Children}[#label-Children]
19
+ - {Task: Create and Add an Element}[#label-Task-3A+Create+and+Add+an+Element]
20
+ - {Task: Add an Existing Element}[#label-Task-3A+Add+an+Existing+Element]
21
+ - {Task: Create and Add an Element with Attributes}[#label-Task-3A+Create+and+Add+an+Element+with+Attributes]
22
+ - {Task: Add an Existing Element with Added Attributes}[#label-Task-3A+Add+an+Existing+Element+with+Added+Attributes]
23
+ - {Task: Delete a Specified Element}[#label-Task-3A+Delete+a+Specified+Element]
24
+ - {Task: Delete an Element by Index}[#label-Task-3A+Delete+an+Element+by+Index]
25
+ - {Task: Delete an Element by XPath}[#label-Task-3A+Delete+an+Element+by+XPath]
26
+ - {Task: Determine Whether Element Children}[#label-Task-3A+Determine+Whether+Element+Children]
27
+ - {Task: Get Element Descendants by XPath}[#label-Task-3A+Get+Element+Descendants+by+XPath]
28
+ - {Task: Get Next Element Sibling}[#label-Task-3A+Get+Next+Element+Sibling]
29
+ - {Task: Get Previous Element Sibling}[#label-Task-3A+Get+Previous+Element+Sibling]
30
+ - {Task: Add a Text Node}[#label-Task-3A+Add+a+Text+Node]
31
+ - {Task: Replace the First Text Node}[#label-Task-3A+Replace+the+First+Text+Node]
32
+ - {Task: Remove the First Text Node}[#label-Task-3A+Remove+the+First+Text+Node]
33
+ - {Task: Retrieve the First Text Node}[#label-Task-3A+Retrieve+the+First+Text+Node]
34
+ - {Task: Retrieve a Specific Text Node}[#label-Task-3A+Retrieve+a+Specific+Text+Node]
35
+ - {Task: Determine Whether the Element has Text Nodes}[#label-Task-3A+Determine+Whether+the+Element+has+Text+Nodes]
36
+ - {Task: Get the Child at a Given Index}[#label-Task-3A+Get+the+Child+at+a+Given+Index]
37
+ - {Task: Get All CDATA Children}[#label-Task-3A+Get+All+CDATA+Children]
38
+ - {Task: Get All Comment Children}[#label-Task-3A+Get+All+Comment+Children]
39
+ - {Task: Get All Processing Instruction Children}[#label-Task-3A+Get+All+Processing+Instruction+Children]
40
+ - {Task: Get All Text Children}[#label-Task-3A+Get+All+Text+Children]
41
+ - {Namespaces}[#label-Namespaces]
42
+ - {Task: Add a Namespace}[#label-Task-3A+Add+a+Namespace]
43
+ - {Task: Delete the Default Namespace}[#label-Task-3A+Delete+the+Default+Namespace]
44
+ - {Task: Delete a Specific Namespace}[#label-Task-3A+Delete+a+Specific+Namespace]
45
+ - {Task: Get a Namespace URI}[#label-Task-3A+Get+a+Namespace+URI]
46
+ - {Task: Retrieve Namespaces}[#label-Task-3A+Retrieve+Namespaces]
47
+ - {Task: Retrieve Namespace Prefixes}[#label-Task-3A+Retrieve+Namespace+Prefixes]
48
+ - {Iteration}[#label-Iteration]
49
+ - {Task: Iterate Over Elements}[#label-Task-3A+Iterate+Over+Elements]
50
+ - {Task: Iterate Over Elements Having a Specified Attribute}[#label-Task-3A+Iterate+Over+Elements+Having+a+Specified+Attribute]
51
+ - {Task: Iterate Over Elements Having a Specified Attribute and Value}[#label-Task-3A+Iterate+Over+Elements+Having+a+Specified+Attribute+and+Value]
52
+ - {Task: Iterate Over Elements Having Specified Text}[#label-Task-3A+Iterate+Over+Elements+Having+Specified+Text]
53
+ - {Context}[#label-Context]
54
+ - {Other Getters}[#label-Other+Getters]
55
+