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.

@@ -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"