rexml 3.2.3 → 3.2.8
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rexml might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/NEWS.md +219 -0
- data/README.md +11 -14
- data/doc/rexml/context.rdoc +143 -0
- data/doc/rexml/tasks/rdoc/child.rdoc +87 -0
- data/doc/rexml/tasks/rdoc/document.rdoc +276 -0
- data/doc/rexml/tasks/rdoc/element.rdoc +602 -0
- data/doc/rexml/tasks/rdoc/node.rdoc +97 -0
- data/doc/rexml/tasks/rdoc/parent.rdoc +267 -0
- data/doc/rexml/tasks/tocs/child_toc.rdoc +12 -0
- data/doc/rexml/tasks/tocs/document_toc.rdoc +30 -0
- data/doc/rexml/tasks/tocs/element_toc.rdoc +55 -0
- data/doc/rexml/tasks/tocs/master_toc.rdoc +135 -0
- data/doc/rexml/tasks/tocs/node_toc.rdoc +16 -0
- data/doc/rexml/tasks/tocs/parent_toc.rdoc +25 -0
- data/doc/rexml/tutorial.rdoc +1358 -0
- data/lib/rexml/attribute.rb +14 -9
- data/lib/rexml/doctype.rb +55 -31
- data/lib/rexml/document.rb +194 -34
- data/lib/rexml/element.rb +1786 -456
- data/lib/rexml/entity.rb +26 -16
- data/lib/rexml/formatters/pretty.rb +2 -2
- data/lib/rexml/functions.rb +1 -2
- data/lib/rexml/light/node.rb +0 -8
- data/lib/rexml/namespace.rb +8 -4
- data/lib/rexml/parseexception.rb +1 -0
- data/lib/rexml/parsers/baseparser.rb +321 -222
- data/lib/rexml/parsers/xpathparser.rb +161 -97
- data/lib/rexml/rexml.rb +29 -22
- data/lib/rexml/source.rb +72 -99
- data/lib/rexml/text.rb +7 -5
- data/lib/rexml/xpath_parser.rb +43 -33
- data/lib/rexml.rb +3 -0
- metadata +43 -34
- data/.gitignore +0 -9
- data/.travis.yml +0 -24
- data/Gemfile +0 -6
- data/Rakefile +0 -8
- data/rexml.gemspec +0 -84
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cef1cb55ed6a23bce0af7d46fce791961558ee885c57f1089207deb53e18ff95
|
4
|
+
data.tar.gz: 4fbe36d9e4422d266c2ffee71fab703e6408e9a2b5533ca0ae49411483f98368
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 60da703f26ff7eaa6cf537634ad68e4c3246605c26c54ea96147fae59bb6c17eeab5c234e5e67301e342472ce8dd3cb671321168cddc212839b9347c8de62f91
|
7
|
+
data.tar.gz: 4d6eeaba460a22bcdc3512ac53504fe87f829cb783069fbebb989fb8f1585fbdfe9ed357c8fbda894fb68777d04f871b9073aa6d13c31eccc6b3b22db10c2ce1
|
data/NEWS.md
CHANGED
@@ -1,5 +1,224 @@
|
|
1
1
|
# News
|
2
2
|
|
3
|
+
## 3.2.8 - 2024-05-16 {#version-3-2-8}
|
4
|
+
|
5
|
+
### Fixes
|
6
|
+
|
7
|
+
* Suppressed a warning
|
8
|
+
|
9
|
+
## 3.2.7 - 2024-05-16 {#version-3-2-7}
|
10
|
+
|
11
|
+
### Improvements
|
12
|
+
|
13
|
+
* Improve parse performance by using `StringScanner`.
|
14
|
+
|
15
|
+
* GH-106
|
16
|
+
* GH-107
|
17
|
+
* GH-108
|
18
|
+
* GH-109
|
19
|
+
* GH-112
|
20
|
+
* GH-113
|
21
|
+
* GH-114
|
22
|
+
* GH-115
|
23
|
+
* GH-116
|
24
|
+
* GH-117
|
25
|
+
* GH-118
|
26
|
+
* GH-119
|
27
|
+
* GH-121
|
28
|
+
|
29
|
+
* Patch by NAITOH Jun.
|
30
|
+
|
31
|
+
* Improved parse performance when an attribute has many `<`s.
|
32
|
+
|
33
|
+
* GH-124
|
34
|
+
|
35
|
+
### Fixes
|
36
|
+
|
37
|
+
* XPath: Fixed a bug of `normalize_space(array)`.
|
38
|
+
|
39
|
+
* GH-110
|
40
|
+
* GH-111
|
41
|
+
|
42
|
+
* Patch by flatisland.
|
43
|
+
|
44
|
+
* XPath: Fixed a bug that wrong position is used with nested path.
|
45
|
+
|
46
|
+
* GH-110
|
47
|
+
* GH-122
|
48
|
+
|
49
|
+
* Reported by jcavalieri.
|
50
|
+
* Patch by NAITOH Jun.
|
51
|
+
|
52
|
+
* Fixed a bug that an exception message can't be generated for
|
53
|
+
invalid encoding XML.
|
54
|
+
|
55
|
+
* GH-29
|
56
|
+
* GH-123
|
57
|
+
|
58
|
+
* Reported by DuKewu.
|
59
|
+
* Patch by NAITOH Jun.
|
60
|
+
|
61
|
+
### Thanks
|
62
|
+
|
63
|
+
* NAITOH Jun
|
64
|
+
* flatisland
|
65
|
+
* jcavalieri
|
66
|
+
* DuKewu
|
67
|
+
|
68
|
+
|
69
|
+
## 3.2.6 - 2023-07-27 {#version-3-2-6}
|
70
|
+
|
71
|
+
### Improvements
|
72
|
+
|
73
|
+
* Required Ruby 2.5 or later explicitly.
|
74
|
+
[GH-69][gh-69]
|
75
|
+
[Patch by Ivo Anjo]
|
76
|
+
|
77
|
+
* Added documentation for maintenance cycle.
|
78
|
+
[GH-71][gh-71]
|
79
|
+
[Patch by Ivo Anjo]
|
80
|
+
|
81
|
+
* Added tutorial.
|
82
|
+
[GH-77][gh-77]
|
83
|
+
[GH-78][gh-78]
|
84
|
+
[Patch by Burdette Lamar]
|
85
|
+
|
86
|
+
* Improved performance and memory usage.
|
87
|
+
[GH-94][gh-94]
|
88
|
+
[Patch by fatkodima]
|
89
|
+
|
90
|
+
* `REXML::Parsers::XPathParser#abbreviate`: Added support for
|
91
|
+
function arguments.
|
92
|
+
[GH-95][gh-95]
|
93
|
+
[Reported by pulver]
|
94
|
+
|
95
|
+
* `REXML::Parsers::XPathParser#abbreviate`: Added support for string
|
96
|
+
literal that contains double-quote.
|
97
|
+
[GH-96][gh-96]
|
98
|
+
[Patch by pulver]
|
99
|
+
|
100
|
+
* `REXML::Parsers::XPathParser#abbreviate`: Added missing `/` to
|
101
|
+
`:descendant_or_self/:self/:parent`.
|
102
|
+
[GH-97][gh-97]
|
103
|
+
[Reported by pulver]
|
104
|
+
|
105
|
+
* `REXML::Parsers::XPathParser#abbreviate`: Added support for more patterns.
|
106
|
+
[GH-97][gh-97]
|
107
|
+
[Reported by pulver]
|
108
|
+
|
109
|
+
### Fixes
|
110
|
+
|
111
|
+
* Fixed a typo in NEWS.
|
112
|
+
[GH-72][gh-72]
|
113
|
+
[Patch by Spencer Goodman]
|
114
|
+
|
115
|
+
* Fixed a typo in NEWS.
|
116
|
+
[GH-75][gh-75]
|
117
|
+
[Patch by Andrew Bromwich]
|
118
|
+
|
119
|
+
* Fixed documents.
|
120
|
+
[GH-87][gh-87]
|
121
|
+
[Patch by Alexander Ilyin]
|
122
|
+
|
123
|
+
* Fixed a bug that `Attriute` convert `'` and `'` even when
|
124
|
+
`attribute_quote: :quote` is used.
|
125
|
+
[GH-92][gh-92]
|
126
|
+
[Reported by Edouard Brière]
|
127
|
+
|
128
|
+
* Fixed links in tutorial.
|
129
|
+
[GH-99][gh-99]
|
130
|
+
[Patch by gemmaro]
|
131
|
+
|
132
|
+
|
133
|
+
### Thanks
|
134
|
+
|
135
|
+
* Ivo Anjo
|
136
|
+
|
137
|
+
* Spencer Goodman
|
138
|
+
|
139
|
+
* Andrew Bromwich
|
140
|
+
|
141
|
+
* Burdette Lamar
|
142
|
+
|
143
|
+
* Alexander Ilyin
|
144
|
+
|
145
|
+
* Edouard Brière
|
146
|
+
|
147
|
+
* fatkodima
|
148
|
+
|
149
|
+
* pulver
|
150
|
+
|
151
|
+
* gemmaro
|
152
|
+
|
153
|
+
[gh-69]:https://github.com/ruby/rexml/issues/69
|
154
|
+
[gh-71]:https://github.com/ruby/rexml/issues/71
|
155
|
+
[gh-72]:https://github.com/ruby/rexml/issues/72
|
156
|
+
[gh-75]:https://github.com/ruby/rexml/issues/75
|
157
|
+
[gh-77]:https://github.com/ruby/rexml/issues/77
|
158
|
+
[gh-87]:https://github.com/ruby/rexml/issues/87
|
159
|
+
[gh-92]:https://github.com/ruby/rexml/issues/92
|
160
|
+
[gh-94]:https://github.com/ruby/rexml/issues/94
|
161
|
+
[gh-95]:https://github.com/ruby/rexml/issues/95
|
162
|
+
[gh-96]:https://github.com/ruby/rexml/issues/96
|
163
|
+
[gh-97]:https://github.com/ruby/rexml/issues/97
|
164
|
+
[gh-98]:https://github.com/ruby/rexml/issues/98
|
165
|
+
[gh-99]:https://github.com/ruby/rexml/issues/99
|
166
|
+
|
167
|
+
## 3.2.5 - 2021-04-05 {#version-3-2-5}
|
168
|
+
|
169
|
+
### Improvements
|
170
|
+
|
171
|
+
* Add more validations to XPath parser.
|
172
|
+
|
173
|
+
* `require "rexml/document"` by default.
|
174
|
+
[GitHub#36][Patch by Koichi ITO]
|
175
|
+
|
176
|
+
* Don't add `#dclone` method to core classes globally.
|
177
|
+
[GitHub#37][Patch by Akira Matsuda]
|
178
|
+
|
179
|
+
* Add more documentations.
|
180
|
+
[Patch by Burdette Lamar]
|
181
|
+
|
182
|
+
* Added `REXML::Elements#parent`.
|
183
|
+
[GitHub#52][Patch by Burdette Lamar]
|
184
|
+
|
185
|
+
### Fixes
|
186
|
+
|
187
|
+
* Fixed a bug that `REXML::DocType#clone` doesn't copy external ID
|
188
|
+
information.
|
189
|
+
|
190
|
+
* Fixed round-trip vulnerability bugs.
|
191
|
+
See also: https://www.ruby-lang.org/en/news/2021/04/05/xml-round-trip-vulnerability-in-rexml-cve-2021-28965/
|
192
|
+
[HackerOne#1104077][CVE-2021-28965][Reported by Juho Nurminen]
|
193
|
+
|
194
|
+
### Thanks
|
195
|
+
|
196
|
+
* Koichi ITO
|
197
|
+
|
198
|
+
* Akira Matsuda
|
199
|
+
|
200
|
+
* Burdette Lamar
|
201
|
+
|
202
|
+
* Juho Nurminen
|
203
|
+
|
204
|
+
## 3.2.4 - 2020-01-31 {#version-3-2-4}
|
205
|
+
|
206
|
+
### Improvements
|
207
|
+
|
208
|
+
* Don't use `taint` with Ruby 2.7 or later.
|
209
|
+
[GitHub#21][Patch by Jeremy Evans]
|
210
|
+
|
211
|
+
### Fixes
|
212
|
+
|
213
|
+
* Fixed a `elsif` typo.
|
214
|
+
[GitHub#22][Patch by Nobuyoshi Nakada]
|
215
|
+
|
216
|
+
### Thanks
|
217
|
+
|
218
|
+
* Jeremy Evans
|
219
|
+
|
220
|
+
* Nobuyoshi Nakada
|
221
|
+
|
3
222
|
## 3.2.3 - 2019-10-12 {#version-3-2-3}
|
4
223
|
|
5
224
|
### Fixes
|
data/README.md
CHANGED
@@ -4,21 +4,9 @@ REXML was inspired by the Electric XML library for Java, which features an easy-
|
|
4
4
|
|
5
5
|
REXML supports both tree and stream document parsing. Stream parsing is faster (about 1.5 times as fast). However, with stream parsing, you don't get access to features such as XPath.
|
6
6
|
|
7
|
-
##
|
7
|
+
## API
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
```ruby
|
12
|
-
gem 'rexml'
|
13
|
-
```
|
14
|
-
|
15
|
-
And then execute:
|
16
|
-
|
17
|
-
$ bundle
|
18
|
-
|
19
|
-
Or install it yourself as:
|
20
|
-
|
21
|
-
$ gem install rexml
|
9
|
+
See the [API documentation](https://ruby.github.io/rexml/).
|
22
10
|
|
23
11
|
## Usage
|
24
12
|
|
@@ -45,6 +33,15 @@ doc = Document.new string
|
|
45
33
|
|
46
34
|
So parsing a string is just as easy as parsing a file.
|
47
35
|
|
36
|
+
## Support
|
37
|
+
|
38
|
+
REXML support follows the same maintenance cycle as Ruby releases, as shown on <https://www.ruby-lang.org/en/downloads/branches/>.
|
39
|
+
|
40
|
+
If you are running on an end-of-life Ruby, do not expect modern REXML releases to be compatible with it; in fact, it's recommended that you DO NOT use this gem, and instead use the REXML version that came bundled with your end-of-life Ruby version.
|
41
|
+
|
42
|
+
The `required_ruby_version` on the gemspec is kept updated on a [best-effort basis](https://github.com/ruby/rexml/pull/70) by the community.
|
43
|
+
Up to version 3.2.5, this information was not set. That version [is known broken with at least Ruby < 2.3](https://github.com/ruby/rexml/issues/69).
|
44
|
+
|
48
45
|
## Development
|
49
46
|
|
50
47
|
After checking out the repo, run `rake test` to run the tests.
|
@@ -0,0 +1,143 @@
|
|
1
|
+
== Element Context
|
2
|
+
|
3
|
+
Notes:
|
4
|
+
- All code on this page presupposes that the following has been executed:
|
5
|
+
|
6
|
+
require 'rexml/document'
|
7
|
+
|
8
|
+
- For convenience, examples on this page use +REXML::Document.new+, not +REXML::Element.new+.
|
9
|
+
This is completely valid, because REXML::Document is a subclass of REXML::Element.
|
10
|
+
|
11
|
+
The context for an element is a hash of processing directives
|
12
|
+
that influence the way \XML is read, stored, and written.
|
13
|
+
The context entries are:
|
14
|
+
|
15
|
+
- +:respect_whitespace+: controls treatment of whitespace.
|
16
|
+
- +:compress_whitespace+: determines whether whitespace is compressed.
|
17
|
+
- +:ignore_whitespace_nodes+: determines whether whitespace-only nodes are to be ignored.
|
18
|
+
- +:raw+: controls treatment of special characters and entities.
|
19
|
+
|
20
|
+
The default context for a new element is <tt>{}</tt>.
|
21
|
+
You can set the context at element-creation time:
|
22
|
+
|
23
|
+
d = REXML::Document.new('', {compress_whitespace: :all, raw: :all})
|
24
|
+
d.context # => {:compress_whitespace=>:all, :raw=>:all}
|
25
|
+
|
26
|
+
You can reset the entire context by assigning a new hash:
|
27
|
+
|
28
|
+
d.context = {ignore_whitespace_nodes: :all}
|
29
|
+
d.context # => {:ignore_whitespace_nodes=>:all}
|
30
|
+
|
31
|
+
Or you can create or modify an individual entry:
|
32
|
+
|
33
|
+
d.context[:raw] = :all
|
34
|
+
d.context # => {:ignore_whitespace_nodes=>:all, :raw=>:all}
|
35
|
+
|
36
|
+
=== +:respect_whitespace+
|
37
|
+
|
38
|
+
Affects: +REXML::Element.new+, +REXML::Element.text=+.
|
39
|
+
|
40
|
+
By default, all parsed whitespace is respected (that is, stored whitespace not compressed):
|
41
|
+
|
42
|
+
xml_string = '<root><foo>a b</foo> <bar>c d</bar> <baz>e f</baz></root>'
|
43
|
+
d = REXML::Document.new(xml_string)
|
44
|
+
d.to_s # => "<root><foo>a b</foo> <bar>c d</bar> <baz>e f</baz></root>"
|
45
|
+
|
46
|
+
Use +:respect_whitespace+ with an array of element names
|
47
|
+
to specify the elements that _are_ to have their whitespace respected;
|
48
|
+
other elements' whitespace, and whitespace between elements, will be compressed.
|
49
|
+
|
50
|
+
In this example: +foo+ and +baz+ will have their whitespace respected;
|
51
|
+
+bar+ and the space between elements will have their whitespace compressed:
|
52
|
+
|
53
|
+
d = REXML::Document.new(xml_string, {respect_whitespace: ['foo', 'baz']})
|
54
|
+
d.to_s # => "<root><foo>a b</foo> <bar>c d</bar> <baz>e f</baz></root>"
|
55
|
+
bar = d.root[2] # => <bar> ... </>
|
56
|
+
bar.text = 'X Y'
|
57
|
+
d.to_s # => "<root><foo>a b</foo> <bar>X Y</bar> <baz>e f</baz></root>"
|
58
|
+
|
59
|
+
=== +:compress_whitespace+
|
60
|
+
|
61
|
+
Affects: +REXML::Element.new+, +REXML::Element.text=+.
|
62
|
+
|
63
|
+
Use <tt>compress_whitespace: :all</tt>
|
64
|
+
to compress whitespace both within and between elements:
|
65
|
+
|
66
|
+
xml_string = '<root><foo>a b</foo> <bar>c d</bar> <baz>e f</baz></root>'
|
67
|
+
d = REXML::Document.new(xml_string, {compress_whitespace: :all})
|
68
|
+
d.to_s # => "<root><foo>a b</foo> <bar>c d</bar> <baz>e f</baz></root>"
|
69
|
+
|
70
|
+
Use +:compress_whitespace+ with an array of element names
|
71
|
+
to compress whitespace in those elements,
|
72
|
+
but not in other elements nor between elements.
|
73
|
+
|
74
|
+
In this example, +foo+ and +baz+ will have their whitespace compressed;
|
75
|
+
+bar+ and the space between elements will not:
|
76
|
+
|
77
|
+
d = REXML::Document.new(xml_string, {compress_whitespace: ['foo', 'baz']})
|
78
|
+
d.to_s # => "<root><foo>a b</foo> <bar>c d</bar> <baz>e f</baz></root>"
|
79
|
+
foo = d.root[0] # => <foo> ... </>
|
80
|
+
foo.text= 'X Y'
|
81
|
+
d.to_s # => "<root><foo>X Y</foo> <bar>c d</bar> <baz>e f</baz></root>"
|
82
|
+
|
83
|
+
=== +:ignore_whitespace_nodes+
|
84
|
+
|
85
|
+
Affects: +REXML::Element.new+.
|
86
|
+
|
87
|
+
Use <tt>ignore_whitespace_nodes: :all</tt> to omit all whitespace-only elements.
|
88
|
+
|
89
|
+
In this example, +bar+ has a text node, while nodes +foo+ and +baz+ do not:
|
90
|
+
|
91
|
+
xml_string = '<root><foo> </foo><bar> BAR </bar><baz> </baz></root>'
|
92
|
+
d = REXML::Document.new(xml_string, {ignore_whitespace_nodes: :all})
|
93
|
+
d.to_s # => "<root><foo> FOO </foo><bar/><baz> BAZ </baz></root>"
|
94
|
+
root = d.root # => <root> ... </>
|
95
|
+
foo = root[0] # => <foo/>
|
96
|
+
bar = root[1] # => <bar> ... </>
|
97
|
+
baz = root[2] # => <baz/>
|
98
|
+
foo.first.class # => NilClass
|
99
|
+
bar.first.class # => REXML::Text
|
100
|
+
baz.first.class # => NilClass
|
101
|
+
|
102
|
+
Use +:ignore_whitespace_nodes+ with an array of element names
|
103
|
+
to specify the elements that are to have whitespace nodes ignored.
|
104
|
+
|
105
|
+
In this example, +bar+ and +baz+ have text nodes, while node +foo+ does not.
|
106
|
+
|
107
|
+
xml_string = '<root><foo> </foo><bar> BAR </bar><baz> </baz></root>'
|
108
|
+
d = REXML::Document.new(xml_string, {ignore_whitespace_nodes: ['foo']})
|
109
|
+
d.to_s # => "<root><foo/><bar> BAR </bar><baz> </baz></root>"
|
110
|
+
root = d.root # => <root> ... </>
|
111
|
+
foo = root[0] # => <foo/>
|
112
|
+
bar = root[1] # => <bar> ... </>
|
113
|
+
baz = root[2] # => <baz> ... </>
|
114
|
+
foo.first.class # => NilClass
|
115
|
+
bar.first.class # => REXML::Text
|
116
|
+
baz.first.class # => REXML::Text
|
117
|
+
|
118
|
+
=== +:raw+
|
119
|
+
|
120
|
+
Affects: +Element.text=+, +Element.add_text+, +Text.to_s+.
|
121
|
+
|
122
|
+
Parsing of +a+ elements is not affected by +raw+:
|
123
|
+
|
124
|
+
xml_string = '<root><a>0 < 1</a><b>1 > 0</b></root>'
|
125
|
+
d = REXML::Document.new(xml_string, {:raw => ['a']})
|
126
|
+
d.root.to_s # => "<root><a>0 < 1</a><b>1 > 0</b></root>"
|
127
|
+
a, b = *d.root.elements
|
128
|
+
a.to_s # => "<a>0 < 1</a>"
|
129
|
+
b.to_s # => "<b>1 > 0</b>"
|
130
|
+
|
131
|
+
But Element#text= is affected:
|
132
|
+
|
133
|
+
a.text = '0 < 1'
|
134
|
+
b.text = '1 > 0'
|
135
|
+
a.to_s # => "<a>0 < 1</a>"
|
136
|
+
b.to_s # => "<b>1 &gt; 0</b>"
|
137
|
+
|
138
|
+
As is Element.add_text:
|
139
|
+
|
140
|
+
a.add_text(' so 1 > 0')
|
141
|
+
b.add_text(' so 0 < 1')
|
142
|
+
a.to_s # => "<a>0 < 1 so 1 > 0</a>"
|
143
|
+
b.to_s # => "<b>1 &gt; 0 so 0 &lt; 1</b>"
|
@@ -0,0 +1,87 @@
|
|
1
|
+
== Class Child
|
2
|
+
|
3
|
+
Class Child includes module Node;
|
4
|
+
see {Tasks for Node}[node_rdoc.html].
|
5
|
+
|
6
|
+
:include: ../tocs/child_toc.rdoc
|
7
|
+
|
8
|
+
=== Relationships
|
9
|
+
|
10
|
+
==== Task: Set the Parent
|
11
|
+
|
12
|
+
Use method {Child#parent=}[../../../../REXML/Parent.html#method-i-parent-3D]
|
13
|
+
to set the parent:
|
14
|
+
|
15
|
+
e0 = REXML::Element.new('foo')
|
16
|
+
e1 = REXML::Element.new('bar')
|
17
|
+
e1.parent # => nil
|
18
|
+
e1.parent = e0
|
19
|
+
e1.parent # => <foo/>
|
20
|
+
|
21
|
+
==== Task: Insert Previous Sibling
|
22
|
+
|
23
|
+
Use method {Child#previous_sibling=}[../../../../REXML/Parent.html#method-i-previous_sibling-3D]
|
24
|
+
to insert a previous sibling:
|
25
|
+
|
26
|
+
xml_string = '<root><a/><c/></root>'
|
27
|
+
d = REXML::Document.new(xml_string)
|
28
|
+
d.root.to_a # => [<a/>, <c/>]
|
29
|
+
c = d.root[1] # => <c/>
|
30
|
+
b = REXML::Element.new('b')
|
31
|
+
c.previous_sibling = b
|
32
|
+
d.root.to_a # => [<a/>, <b/>, <c/>]
|
33
|
+
|
34
|
+
==== Task: Insert Next Sibling
|
35
|
+
|
36
|
+
Use method {Child#next_sibling=}[../../../../REXML/Parent.html#method-i-next-sibling-3D]
|
37
|
+
to insert a previous sibling:
|
38
|
+
|
39
|
+
xml_string = '<root><a/><c/></root>'
|
40
|
+
d = REXML::Document.new(xml_string)
|
41
|
+
d.root.to_a # => [<a/>, <c/>]
|
42
|
+
a = d.root[0] # => <a/>
|
43
|
+
b = REXML::Element.new('b')
|
44
|
+
a.next_sibling = b
|
45
|
+
d.root.to_a # => [<a/>, <b/>, <c/>]
|
46
|
+
|
47
|
+
=== Removal or Replacement
|
48
|
+
|
49
|
+
==== Task: Remove Child from Parent
|
50
|
+
|
51
|
+
Use method {Child#remove}[../../../../REXML/Parent.html#method-i-remove]
|
52
|
+
to remove a child from its parent; returns the removed child:
|
53
|
+
|
54
|
+
xml_string = '<root><a/><b/><c/></root>'
|
55
|
+
d = REXML::Document.new(xml_string)
|
56
|
+
d.root.to_a # => [<a/>, <b/>, <c/>]
|
57
|
+
b = d.root[1] # => <b/>
|
58
|
+
b.remove # => <b/>
|
59
|
+
d.root.to_a # => [<a/>, <c/>]
|
60
|
+
|
61
|
+
==== Task: Replace Child
|
62
|
+
|
63
|
+
Use method {Child#replace_with}[../../../../REXML/Parent.html#method-i-replace]
|
64
|
+
to replace a child;
|
65
|
+
returns the replaced child:
|
66
|
+
|
67
|
+
xml_string = '<root><a/><b/><c/></root>'
|
68
|
+
d = REXML::Document.new(xml_string)
|
69
|
+
d.root.to_a # => [<a/>, <b/>, <c/>]
|
70
|
+
b = d.root[1] # => <b/>
|
71
|
+
d = REXML::Element.new('d')
|
72
|
+
b.replace_with(d) # => <b/>
|
73
|
+
d.root.to_a # => [<a/>, <d/>, <c/>]
|
74
|
+
|
75
|
+
=== Document
|
76
|
+
|
77
|
+
==== Task: Get the Document
|
78
|
+
|
79
|
+
Use method {Child#document}[../../../../REXML/Parent.html#method-i-document]
|
80
|
+
to get the document for the child:
|
81
|
+
|
82
|
+
xml_string = '<root><a/><b/><c/></root>'
|
83
|
+
d = REXML::Document.new(xml_string)
|
84
|
+
d.root.to_a # => [<a/>, <b/>, <c/>]
|
85
|
+
b = d.root[1] # => <b/>
|
86
|
+
b.document == d # => true
|
87
|
+
REXML::Child.new.document # => nil
|