rexml 3.2.4 → 3.2.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e09eaebc2e8b94e90c9529f88060a7aa21cbaed09fe2f424d1293cf5a9e19785
4
- data.tar.gz: 3c9d5d8a428c10037df1b93de84f9420b31caccd435d903361f7d9d3b5b49da5
3
+ metadata.gz: 2583ae302aa5e698f0887a689c416e5debe0533ac472a9f96fce6a8912040fd8
4
+ data.tar.gz: b0ffa6301fd899969a78e060ccaeafebfc2169e3c63ff499ebc6170468866475
5
5
  SHA512:
6
- metadata.gz: 17f5224c52c6ec2f5388545f34167f486a826eeba5d9bd83f69e7bf59e194e2ee8d738a6625749dd282961b1c4ec2f3d028cd7935eff58dc3882a5272b440c5c
7
- data.tar.gz: 04e0b736f5d40c3585582ffa3db301a94c116ff3d2152f911414b04917979e3c9defca5c5f98088962cde6491adec37bc6d774b5e8693cd954148d7d6fc22268
6
+ metadata.gz: f63fb0b84ef51e790cc6310244f2106d8c47ec9a00687c58c743afda82b60be9986d503c6f56f947db06f6758707facccd03405c4d1009376e856080aa26d0e4
7
+ data.tar.gz: db62bea7391837a7ab4cfc5cb5a412ed4deb8d232653ca66d93a323a5a76383eed520cd4ced5b20204f29b04e84678791cd6f807195868f5d4a5e519a73d2aaf
data/NEWS.md CHANGED
@@ -1,5 +1,140 @@
1
1
  # News
2
2
 
3
+ ## 3.2.6 - 2023-07-27 {#version-3-2-6}
4
+
5
+ ### Improvements
6
+
7
+ * Required Ruby 2.5 or later explicitly.
8
+ [GH-69][gh-69]
9
+ [Patch by Ivo Anjo]
10
+
11
+ * Added documentation for maintenance cycle.
12
+ [GH-71][gh-71]
13
+ [Patch by Ivo Anjo]
14
+
15
+ * Added tutorial.
16
+ [GH-77][gh-77]
17
+ [GH-78][gh-78]
18
+ [Patch by Burdette Lamar]
19
+
20
+ * Improved performance and memory usage.
21
+ [GH-94][gh-94]
22
+ [Patch by fatkodima]
23
+
24
+ * `REXML::Parsers::XPathParser#abbreviate`: Added support for
25
+ function arguments.
26
+ [GH-95][gh-95]
27
+ [Reported by pulver]
28
+
29
+ * `REXML::Parsers::XPathParser#abbreviate`: Added support for string
30
+ literal that contains double-quote.
31
+ [GH-96][gh-96]
32
+ [Patch by pulver]
33
+
34
+ * `REXML::Parsers::XPathParser#abbreviate`: Added missing `/` to
35
+ `:descendant_or_self/:self/:parent`.
36
+ [GH-97][gh-97]
37
+ [Reported by pulver]
38
+
39
+ * `REXML::Parsers::XPathParser#abbreviate`: Added support for more patterns.
40
+ [GH-97][gh-97]
41
+ [Reported by pulver]
42
+
43
+ ### Fixes
44
+
45
+ * Fixed a typo in NEWS.
46
+ [GH-72][gh-72]
47
+ [Patch by Spencer Goodman]
48
+
49
+ * Fixed a typo in NEWS.
50
+ [GH-75][gh-75]
51
+ [Patch by Andrew Bromwich]
52
+
53
+ * Fixed documents.
54
+ [GH-87][gh-87]
55
+ [Patch by Alexander Ilyin]
56
+
57
+ * Fixed a bug that `Attriute` convert `'` and `'` even when
58
+ `attribute_quote: :quote` is used.
59
+ [GH-92][gh-92]
60
+ [Reported by Edouard Brière]
61
+
62
+ * Fixed links in tutorial.
63
+ [GH-99][gh-99]
64
+ [Patch by gemmaro]
65
+
66
+
67
+ ### Thanks
68
+
69
+ * Ivo Anjo
70
+
71
+ * Spencer Goodman
72
+
73
+ * Andrew Bromwich
74
+
75
+ * Burdette Lamar
76
+
77
+ * Alexander Ilyin
78
+
79
+ * Edouard Brière
80
+
81
+ * fatkodima
82
+
83
+ * pulver
84
+
85
+ * gemmaro
86
+
87
+ [gh-69]:https://github.com/ruby/rexml/issues/69
88
+ [gh-71]:https://github.com/ruby/rexml/issues/71
89
+ [gh-72]:https://github.com/ruby/rexml/issues/72
90
+ [gh-75]:https://github.com/ruby/rexml/issues/75
91
+ [gh-77]:https://github.com/ruby/rexml/issues/77
92
+ [gh-87]:https://github.com/ruby/rexml/issues/87
93
+ [gh-92]:https://github.com/ruby/rexml/issues/92
94
+ [gh-94]:https://github.com/ruby/rexml/issues/94
95
+ [gh-95]:https://github.com/ruby/rexml/issues/95
96
+ [gh-96]:https://github.com/ruby/rexml/issues/96
97
+ [gh-97]:https://github.com/ruby/rexml/issues/97
98
+ [gh-98]:https://github.com/ruby/rexml/issues/98
99
+ [gh-99]:https://github.com/ruby/rexml/issues/99
100
+
101
+ ## 3.2.5 - 2021-04-05 {#version-3-2-5}
102
+
103
+ ### Improvements
104
+
105
+ * Add more validations to XPath parser.
106
+
107
+ * `require "rexml/document"` by default.
108
+ [GitHub#36][Patch by Koichi ITO]
109
+
110
+ * Don't add `#dclone` method to core classes globally.
111
+ [GitHub#37][Patch by Akira Matsuda]
112
+
113
+ * Add more documentations.
114
+ [Patch by Burdette Lamar]
115
+
116
+ * Added `REXML::Elements#parent`.
117
+ [GitHub#52][Patch by Burdette Lamar]
118
+
119
+ ### Fixes
120
+
121
+ * Fixed a bug that `REXML::DocType#clone` doesn't copy external ID
122
+ information.
123
+
124
+ * Fixed round-trip vulnerability bugs.
125
+ See also: https://www.ruby-lang.org/en/news/2021/04/05/xml-round-trip-vulnerability-in-rexml-cve-2021-28965/
126
+ [HackerOne#1104077][CVE-2021-28965][Reported by Juho Nurminen]
127
+
128
+ ### Thanks
129
+
130
+ * Koichi ITO
131
+
132
+ * Akira Matsuda
133
+
134
+ * Burdette Lamar
135
+
136
+ * Juho Nurminen
137
+
3
138
  ## 3.2.4 - 2020-01-31 {#version-3-2-4}
4
139
 
5
140
  ### Improvements
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
- ## Installation
7
+ ## API
8
8
 
9
- Add this line to your application's Gemfile:
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 &lt; 1</a><b>1 &gt; 0</b></root>'
125
+ d = REXML::Document.new(xml_string, {:raw => ['a']})
126
+ d.root.to_s # => "<root><a>0 &lt; 1</a><b>1 &gt; 0</b></root>"
127
+ a, b = *d.root.elements
128
+ a.to_s # => "<a>0 &lt; 1</a>"
129
+ b.to_s # => "<b>1 &gt; 0</b>"
130
+
131
+ But Element#text= is affected:
132
+
133
+ a.text = '0 &lt; 1'
134
+ b.text = '1 &gt; 0'
135
+ a.to_s # => "<a>0 &lt; 1</a>"
136
+ b.to_s # => "<b>1 &amp;gt; 0</b>"
137
+
138
+ As is Element.add_text:
139
+
140
+ a.add_text(' so 1 &gt; 0')
141
+ b.add_text(' so 0 &lt; 1')
142
+ a.to_s # => "<a>0 &lt; 1 so 1 &gt; 0</a>"
143
+ b.to_s # => "<b>1 &amp;gt; 0 so 0 &amp;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
@@ -0,0 +1,276 @@
1
+ == Class Document
2
+
3
+ Class Document has methods from its superclasses and included modules;
4
+ see:
5
+
6
+ - {Tasks for Element}[element_rdoc.html].
7
+ - {Tasks for Parent}[parent_rdoc.html].
8
+ - {Tasks for Child}[child_rdoc.html].
9
+ - {Tasks for Node}[node_rdoc.html].
10
+ - {Module Enumerable}[https://docs.ruby-lang.org/en/master/Enumerable.html].
11
+
12
+ :include: ../tocs/document_toc.rdoc
13
+
14
+ === New Document
15
+
16
+ ==== Task: Create an Empty Document
17
+
18
+ Use method {Document::new}[../../../../REXML/Document.html#method-c-new]
19
+ to create an empty document.
20
+
21
+ d = REXML::Document.new
22
+
23
+ ==== Task: Parse a \String into a New Document
24
+
25
+ Use method {Document::new}[../../../../REXML/Document.html#method-c-new]
26
+ to parse an XML string into a new document:
27
+
28
+ xml_string = '<root><a/>text<b/>more<c/></root>'
29
+ d = REXML::Document.new(xml_string)
30
+ d.root # => <root> ... </>
31
+
32
+ ==== Task: Parse an \IO Stream into a New Document
33
+
34
+ Use method {Document::new}[../../../../REXML/Document.html#method-c-new]
35
+ to parse an XML \IO stream into a new document:
36
+
37
+ xml_string = '<root><a/>text<b/>more<c/></root>'
38
+ File.write('t.xml', xml_string)
39
+ d = File.open('t.xml', 'r') do |file|
40
+ REXML::Document.new(file)
41
+ end
42
+ d.root # => <root> ... </>
43
+
44
+ ==== Task: Create a Document from an Existing Document
45
+
46
+ Use method {Document::new}[../../../../REXML/Document.html#method-c-new]
47
+ to create a document from an existing document.
48
+ The context and attributes are copied to the new document,
49
+ but not the children:
50
+
51
+ xml_string = '<root><a/>text<b/>more<c/></root>'
52
+ d = REXML::Document.new(xml_string)
53
+ d.children # => [<root> ... </>]
54
+ d.context = {raw: :all, compress_whitespace: :all}
55
+ d.add_attributes({'bar' => 0, 'baz' => 1})
56
+ d1 = REXML::Document.new(d)
57
+ d1.context # => {:raw=>:all, :compress_whitespace=>:all}
58
+ d1.attributes # => {"bar"=>bar='0', "baz"=>baz='1'}
59
+ d1.children # => []
60
+
61
+ ==== Task: Clone a Document
62
+
63
+ Use method {Document#clone}[../../../../REXML/Document.html#method-i-clone]
64
+ to clone a document.
65
+ The context and attributes are copied to the new document,
66
+ but not the children:
67
+
68
+ xml_string = '<root><a/>text<b/>more<c/></root>'
69
+ d = REXML::Document.new(xml_string)
70
+ d.children # => [<root> ... </>]
71
+ d.context = {raw: :all, compress_whitespace: :all}
72
+ d.add_attributes({'bar' => 0, 'baz' => 1})
73
+ d1 = d.clone # => < bar='0' baz='1'/>
74
+ d1.context # => {:raw=>:all, :compress_whitespace=>:all}
75
+ d1.attributes # => {"bar"=>bar='0', "baz"=>baz='1'}
76
+ d1.children # => []
77
+
78
+ === Document Type
79
+
80
+ ==== Task: Get the Document Type
81
+
82
+ Use method {Document#doctype}[../../../../REXML/Document.html#method-i-doctype]
83
+ to get the document type:
84
+
85
+ d = REXML::Document.new('<!DOCTYPE document SYSTEM "subjects.dtd">')
86
+ d.doctype.class # => REXML::DocType
87
+ d = REXML::Document.new('')
88
+ d.doctype.class # => nil
89
+
90
+ ==== Task: Set the Document Type
91
+
92
+ Use method {document#add}[../../../../REXML/Document.html#method-i-add]
93
+ to add or replace the document type:
94
+
95
+ d = REXML::Document.new('')
96
+ d.doctype.class # => nil
97
+ d.add(REXML::DocType.new('foo'))
98
+ d.doctype.class # => REXML::DocType
99
+
100
+ === XML Declaration
101
+
102
+ ==== Task: Get the XML Declaration
103
+
104
+ Use method {document#xml_decl}[../../../../REXML/Document.html#method-i-xml_decl]
105
+ to get the XML declaration:
106
+
107
+ d = REXML::Document.new('<!DOCTYPE document SYSTEM "subjects.dtd">')
108
+ d.xml_decl.class # => REXML::XMLDecl
109
+ d.xml_decl # => <?xml ... ?>
110
+ d = REXML::Document.new('')
111
+ d.xml_decl.class # => REXML::XMLDecl
112
+ d.xml_decl # => <?xml ... ?>
113
+
114
+ ==== Task: Set the XML Declaration
115
+
116
+ Use method {document#add}[../../../../REXML/Document.html#method-i-add]
117
+ to replace the XML declaration:
118
+
119
+ d = REXML::Document.new('<!DOCTYPE document SYSTEM "subjects.dtd">')
120
+ d.add(REXML::XMLDecl.new)
121
+
122
+ === Children
123
+
124
+ ==== Task: Add an Element Child
125
+
126
+ Use method
127
+ {document#add_element}[../../../../REXML/Document.html#method-i-add_element]
128
+ to add an element to the document:
129
+
130
+ d = REXML::Document.new('')
131
+ d.add_element(REXML::Element.new('root'))
132
+ d.children # => [<root/>]
133
+
134
+ ==== Task: Add a Non-Element Child
135
+
136
+ Use method
137
+ {document#add}[../../../../REXML/Document.html#method-i-add]
138
+ to add a non-element to the document:
139
+
140
+ xml_string = '<root><a/>text<b/>more<c/></root>'
141
+ d = REXML::Document.new(xml_string)
142
+ d.add(REXML::Text.new('foo'))
143
+ d.children # => [<root> ... </>, "foo"]
144
+
145
+ === Writing
146
+
147
+ ==== Task: Write to $stdout
148
+
149
+ Use method
150
+ {document#write}[../../../../REXML/Document.html#method-i-write]
151
+ to write the document to <tt>$stdout</tt>:
152
+
153
+ xml_string = '<root><a/>text<b/>more<c/></root>'
154
+ d = REXML::Document.new(xml_string)
155
+ d.write
156
+
157
+ Output:
158
+
159
+ <root><a/>text<b/>more<c/></root>
160
+
161
+ ==== Task: Write to IO Stream
162
+
163
+ Use method
164
+ {document#write}[../../../../REXML/Document.html#method-i-write]
165
+ to write the document to <tt>$stdout</tt>:
166
+
167
+ xml_string = '<root><a/>text<b/>more<c/></root>'
168
+ d = REXML::Document.new(xml_string)
169
+ File.open('t.xml', 'w') do |file|
170
+ d.write(file)
171
+ end
172
+ p File.read('t.xml')
173
+
174
+ Output:
175
+
176
+ "<root><a/>text<b/>more<c/></root>"
177
+
178
+ ==== Task: Write with No Indentation
179
+
180
+ Use method
181
+ {document#write}[../../../../REXML/Document.html#method-i-write]
182
+ to write the document with no indentation:
183
+
184
+ xml_string = '<root><a><b><c></c></b></a></root>'
185
+ d = REXML::Document.new(xml_string)
186
+ d.write({indent: 0})
187
+
188
+ Output:
189
+
190
+ <root>
191
+ <a>
192
+ <b>
193
+ <c/>
194
+ </b>
195
+ </a>
196
+ </root>
197
+
198
+ ==== Task: Write with Specified Indentation
199
+
200
+ Use method
201
+ {document#write}[../../../../REXML/Document.html#method-i-write]
202
+ to write the document with a specified indentation:
203
+
204
+ xml_string = '<root><a><b><c></c></b></a></root>'
205
+ d = REXML::Document.new(xml_string)
206
+ d.write({indent: 2})
207
+
208
+ Output:
209
+
210
+ <root>
211
+ <a>
212
+ <b>
213
+ <c/>
214
+ </b>
215
+ </a>
216
+ </root>
217
+
218
+ === Querying
219
+
220
+ ==== Task: Get the Document
221
+
222
+ Use method
223
+ {document#document}[../../../../REXML/Document.html#method-i-document]
224
+ to get the document (+self+); overrides <tt>Element#document</tt>:
225
+
226
+ xml_string = '<root><a><b><c></c></b></a></root>'
227
+ d = REXML::Document.new(xml_string)
228
+ d.document == d # => true
229
+
230
+ ==== Task: Get the Encoding
231
+
232
+ Use method
233
+ {document#document}[../../../../REXML/Document.html#method-i-document]
234
+ to get the document (+self+); overrides <tt>Element#document</tt>:
235
+
236
+ xml_string = '<root><a><b><c></c></b></a></root>'
237
+ d = REXML::Document.new(xml_string)
238
+ d.encoding # => "UTF-8"
239
+
240
+ ==== Task: Get the Node Type
241
+
242
+ Use method
243
+ {document#node_type}[../../../../REXML/Document.html#method-i-node_type]
244
+ to get the node type (+:document+); overrides <tt>Element#node_type</tt>:
245
+
246
+ xml_string = '<root><a><b><c></c></b></a></root>'
247
+ d = REXML::Document.new(xml_string)
248
+ d.node_type # => :document
249
+
250
+ ==== Task: Get the Root Element
251
+
252
+ Use method
253
+ {document#root}[../../../../REXML/Document.html#method-i-root]
254
+ to get the root element:
255
+
256
+ xml_string = '<root><a><b><c></c></b></a></root>'
257
+ d = REXML::Document.new(xml_string)
258
+ d.root # => <root> ... </>
259
+
260
+ ==== Task: Determine Whether Stand-Alone
261
+
262
+ Use method
263
+ {document#stand_alone?}[../../../../REXML/Document.html#method-i-stand_alone-3F]
264
+ to get the stand-alone value:
265
+
266
+ d = REXML::Document.new('<?xml standalone="yes"?>')
267
+ d.stand_alone? # => "yes"
268
+
269
+ ==== Task: Get the Version
270
+
271
+ Use method
272
+ {document#version}[../../../../REXML/Document.html#method-i-version]
273
+ to get the version:
274
+
275
+ d = REXML::Document.new('<?xml version="2.0" encoding="UTF-8"?>')
276
+ d.version # => "2.0"