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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
== Class Document
Class Document has methods from its superclasses and included modules;
see:
- {Tasks for Element}[element_rdoc.html].
- {Tasks for Parent}[parent_rdoc.html].
- {Tasks for Child}[child_rdoc.html].
- {Tasks for Node}[node_rdoc.html].
- {Module Enumerable}[https://docs.ruby-lang.org/en/master/Enumerable.html].
:include: ../tocs/document_toc.rdoc
=== New Document
==== Task: Create an Empty Document
Use method {Document::new}[../../../../REXML/Document.html#method-c-new]
to create an empty document.
d = REXML::Document.new
==== Task: Parse a \String into a New Document
Use method {Document::new}[../../../../REXML/Document.html#method-c-new]
to parse an XML string into a new document:
xml_string = '<root><a/>text<b/>more<c/></root>'
d = REXML::Document.new(xml_string)
d.root # => <root> ... </>
==== Task: Parse an \IO Stream into a New Document
Use method {Document::new}[../../../../REXML/Document.html#method-c-new]
to parse an XML \IO stream into a new document:
xml_string = '<root><a/>text<b/>more<c/></root>'
File.write('t.xml', xml_string)
d = File.open('t.xml', 'r') do |file|
REXML::Document.new(file)
end
d.root # => <root> ... </>
==== Task: Create a Document from an Existing Document
Use method {Document::new}[../../../../REXML/Document.html#method-c-new]
to create a document from an existing document.
The context and attributes are copied to the new document,
but not the children:
xml_string = '<root><a/>text<b/>more<c/></root>'
d = REXML::Document.new(xml_string)
d.children # => [<root> ... </>]
d.context = {raw: :all, compress_whitespace: :all}
d.add_attributes({'bar' => 0, 'baz' => 1})
d1 = REXML::Document.new(d)
d1.context # => {:raw=>:all, :compress_whitespace=>:all}
d1.attributes # => {"bar"=>bar='0', "baz"=>baz='1'}
d1.children # => []
==== Task: Clone a Document
Use method {Document#clone}[../../../../REXML/Document.html#method-i-clone]
to clone a document.
The context and attributes are copied to the new document,
but not the children:
xml_string = '<root><a/>text<b/>more<c/></root>'
d = REXML::Document.new(xml_string)
d.children # => [<root> ... </>]
d.context = {raw: :all, compress_whitespace: :all}
d.add_attributes({'bar' => 0, 'baz' => 1})
d1 = d.clone # => < bar='0' baz='1'/>
d1.context # => {:raw=>:all, :compress_whitespace=>:all}
d1.attributes # => {"bar"=>bar='0', "baz"=>baz='1'}
d1.children # => []
=== Document Type
==== Task: Get the Document Type
Use method {Document#doctype}[../../../../REXML/Document.html#method-i-doctype]
to get the document type:
d = REXML::Document.new('<!DOCTYPE document SYSTEM "subjects.dtd">')
d.doctype.class # => REXML::DocType
d = REXML::Document.new('')
d.doctype.class # => nil
==== Task: Set the Document Type
Use method {document#add}[../../../../REXML/Document.html#method-i-add]
to add or replace the document type:
d = REXML::Document.new('')
d.doctype.class # => nil
d.add(REXML::DocType.new('foo'))
d.doctype.class # => REXML::DocType
=== XML Declaration
==== Task: Get the XML Declaration
Use method {document#xml_decl}[../../../../REXML/Document.html#method-i-xml_decl]
to get the XML declaration:
d = REXML::Document.new('<!DOCTYPE document SYSTEM "subjects.dtd">')
d.xml_decl.class # => REXML::XMLDecl
d.xml_decl # => <?xml ... ?>
d = REXML::Document.new('')
d.xml_decl.class # => REXML::XMLDecl
d.xml_decl # => <?xml ... ?>
==== Task: Set the XML Declaration
Use method {document#add}[../../../../REXML/Document.html#method-i-add]
to replace the XML declaration:
d = REXML::Document.new('<!DOCTYPE document SYSTEM "subjects.dtd">')
d.add(REXML::XMLDecl.new)
=== Children
==== Task: Add an Element Child
Use method
{document#add_element}[../../../../REXML/Document.html#method-i-add_element]
to add an element to the document:
d = REXML::Document.new('')
d.add_element(REXML::Element.new('root'))
d.children # => [<root/>]
==== Task: Add a Non-Element Child
Use method
{document#add}[../../../../REXML/Document.html#method-i-add]
to add a non-element to the document:
xml_string = '<root><a/>text<b/>more<c/></root>'
d = REXML::Document.new(xml_string)
d.add(REXML::Text.new('foo'))
d.children # => [<root> ... </>, "foo"]
=== Writing
==== Task: Write to $stdout
Use method
{document#write}[../../../../REXML/Document.html#method-i-write]
to write the document to <tt>$stdout</tt>:
xml_string = '<root><a/>text<b/>more<c/></root>'
d = REXML::Document.new(xml_string)
d.write
Output:
<root><a/>text<b/>more<c/></root>
==== Task: Write to IO Stream
Use method
{document#write}[../../../../REXML/Document.html#method-i-write]
to write the document to <tt>$stdout</tt>:
xml_string = '<root><a/>text<b/>more<c/></root>'
d = REXML::Document.new(xml_string)
File.open('t.xml', 'w') do |file|
d.write(file)
end
p File.read('t.xml')
Output:
"<root><a/>text<b/>more<c/></root>"
==== Task: Write with No Indentation
Use method
{document#write}[../../../../REXML/Document.html#method-i-write]
to write the document with no indentation:
xml_string = '<root><a><b><c></c></b></a></root>'
d = REXML::Document.new(xml_string)
d.write({indent: 0})
Output:
<root>
<a>
<b>
<c/>
</b>
</a>
</root>
==== Task: Write with Specified Indentation
Use method
{document#write}[../../../../REXML/Document.html#method-i-write]
to write the document with a specified indentation:
xml_string = '<root><a><b><c></c></b></a></root>'
d = REXML::Document.new(xml_string)
d.write({indent: 2})
Output:
<root>
<a>
<b>
<c/>
</b>
</a>
</root>
=== Querying
==== Task: Get the Document
Use method
{document#document}[../../../../REXML/Document.html#method-i-document]
to get the document (+self+); overrides <tt>Element#document</tt>:
xml_string = '<root><a><b><c></c></b></a></root>'
d = REXML::Document.new(xml_string)
d.document == d # => true
==== Task: Get the Encoding
Use method
{document#document}[../../../../REXML/Document.html#method-i-document]
to get the document (+self+); overrides <tt>Element#document</tt>:
xml_string = '<root><a><b><c></c></b></a></root>'
d = REXML::Document.new(xml_string)
d.encoding # => "UTF-8"
==== Task: Get the Node Type
Use method
{document#node_type}[../../../../REXML/Document.html#method-i-node_type]
to get the node type (+:document+); overrides <tt>Element#node_type</tt>:
xml_string = '<root><a><b><c></c></b></a></root>'
d = REXML::Document.new(xml_string)
d.node_type # => :document
==== Task: Get the Root Element
Use method
{document#root}[../../../../REXML/Document.html#method-i-root]
to get the root element:
xml_string = '<root><a><b><c></c></b></a></root>'
d = REXML::Document.new(xml_string)
d.root # => <root> ... </>
==== Task: Determine Whether Stand-Alone
Use method
{document#stand_alone?}[../../../../REXML/Document.html#method-i-stand_alone-3F]
to get the stand-alone value:
d = REXML::Document.new('<?xml standalone="yes"?>')
d.stand_alone? # => "yes"
==== Task: Get the Version
Use method
{document#version}[../../../../REXML/Document.html#method-i-version]
to get the version:
d = REXML::Document.new('<?xml version="2.0" encoding="UTF-8"?>')
d.version # => "2.0"
|