91 * w.endDocument(); |
91 * w.endDocument(); |
92 * </pre> |
92 * </pre> |
93 * |
93 * |
94 * <p>The resulting document will look like this:</p> |
94 * <p>The resulting document will look like this:</p> |
95 * |
95 * |
96 * <pre> |
96 * <pre>{@code |
97 * <?xml version="1.0" standalone="yes"?> |
97 * <?xml version="1.0" standalone="yes"?> |
98 * |
98 * |
99 * <greeting>Hello, world!</greeting> |
99 * <greeting>Hello, world!</greeting> |
100 * </pre> |
100 * }</pre> |
101 * |
101 * |
102 * <p>In fact, there is an even simpler convenience method, |
102 * <p>In fact, there is an even simpler convenience method, |
103 * <var>dataElement</var>, designed for writing elements that |
103 * <var>dataElement</var>, designed for writing elements that |
104 * contain only character data, so the code to generate the |
104 * contain only character data, so the code to generate the |
105 * document could be shortened to</p> |
105 * document could be shortened to</p> |
125 * w.dataElement("item", "3"); |
125 * w.dataElement("item", "3"); |
126 * </pre> |
126 * </pre> |
127 * |
127 * |
128 * <p>you will end up with</p> |
128 * <p>you will end up with</p> |
129 * |
129 * |
130 * <pre> |
130 * <pre>{@code |
131 * <item>1</item><item>3</item><item>3</item> |
131 * <item>1</item><item>3</item><item>3</item> |
132 * </pre> |
132 * }</pre> |
133 * |
133 * |
134 * <p>You need to invoke one of the <var>characters</var> methods |
134 * <p>You need to invoke one of the <var>characters</var> methods |
135 * explicitly to add newlines or indentation. Alternatively, you |
135 * explicitly to add newlines or indentation. Alternatively, you |
136 * can use {@link DataWriter}, which |
136 * can use {@link DataWriter}, which |
137 * is derived from this class -- it is optimized for writing |
137 * is derived from this class -- it is optimized for writing |
154 * w.endDocument(); |
154 * w.endDocument(); |
155 * </pre> |
155 * </pre> |
156 * |
156 * |
157 * <p>The resulting document will look like this:</p> |
157 * <p>The resulting document will look like this:</p> |
158 * |
158 * |
159 * <pre> |
159 * <pre>{@code |
160 * <?xml version="1.0" standalone="yes"?> |
160 * <?xml version="1.0" standalone="yes"?> |
161 * |
161 * |
162 * <_NS1:foo xmlns:_NS1="http://www.foo.com/ns/"/> |
162 * <_NS1:foo xmlns:_NS1="http://www.foo.com/ns/"/> |
163 * </pre> |
163 * }</pre> |
164 * |
164 * |
165 * <p>In many cases, document authors will prefer to choose their |
165 * <p>In many cases, document authors will prefer to choose their |
166 * own prefixes rather than using the (ugly) default names. The |
166 * own prefixes rather than using the (ugly) default names. The |
167 * XML writer allows two methods for selecting prefixes:</p> |
167 * XML writer allows two methods for selecting prefixes:</p> |
168 * |
168 * |
186 * w.endDocument(); |
186 * w.endDocument(); |
187 * </pre> |
187 * </pre> |
188 * |
188 * |
189 * <p>The resulting document will look like this:</p> |
189 * <p>The resulting document will look like this:</p> |
190 * |
190 * |
191 * <pre> |
191 * <pre>{@code |
192 * <?xml version="1.0" standalone="yes"?> |
192 * <?xml version="1.0" standalone="yes"?> |
193 * |
193 * |
194 * <foo:foo xmlns:foo="http://www.foo.com/ns/"/> |
194 * <foo:foo xmlns:foo="http://www.foo.com/ns/"/> |
195 * </pre> |
195 * }</pre> |
196 * |
196 * |
197 * <p>The default Namespace simply uses an empty string as the prefix:</p> |
197 * <p>The default Namespace simply uses an empty string as the prefix:</p> |
198 * |
198 * |
199 * <pre> |
199 * <pre> |
200 * w.setPrefix("http://www.foo.com/ns/", ""); |
200 * w.setPrefix("http://www.foo.com/ns/", ""); |
203 * w.endDocument(); |
203 * w.endDocument(); |
204 * </pre> |
204 * </pre> |
205 * |
205 * |
206 * <p>The resulting document will look like this:</p> |
206 * <p>The resulting document will look like this:</p> |
207 * |
207 * |
208 * <pre> |
208 * <pre>{@code |
209 * <?xml version="1.0" standalone="yes"?> |
209 * <?xml version="1.0" standalone="yes"?> |
210 * |
210 * |
211 * <foo xmlns="http://www.foo.com/ns/"/> |
211 * <foo xmlns="http://www.foo.com/ns/"/> |
212 * </pre> |
212 * }</pre> |
213 * |
213 * |
214 * <p>By default, the XML writer will not declare a Namespace until |
214 * <p>By default, the XML writer will not declare a Namespace until |
215 * it is actually used. Sometimes, this approach will create |
215 * it is actually used. Sometimes, this approach will create |
216 * a large number of Namespace declarations, as in the following |
216 * a large number of Namespace declarations, as in the following |
217 * example:</p> |
217 * example:</p> |
218 * |
218 * |
219 * <pre> |
219 * <pre>{@code |
220 * <xml version="1.0" standalone="yes"?> |
220 * <xml version="1.0" standalone="yes"?> |
221 * |
221 * |
222 * <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> |
222 * <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> |
223 * <rdf:Description about="http://www.foo.com/ids/books/12345"> |
223 * <rdf:Description about="http://www.foo.com/ids/books/12345"> |
224 * <dc:title xmlns:dc="http://www.purl.org/dc/">A Dark Night</dc:title> |
224 * <dc:title xmlns:dc="http://www.purl.org/dc/">A Dark Night</dc:title> |
225 * <dc:creator xmlns:dc="http://www.purl.org/dc/">Jane Smith</dc:title> |
225 * <dc:creator xmlns:dc="http://www.purl.org/dc/">Jane Smith</dc:title> |
226 * <dc:date xmlns:dc="http://www.purl.org/dc/">2000-09-09</dc:title> |
226 * <dc:date xmlns:dc="http://www.purl.org/dc/">2000-09-09</dc:title> |
227 * </rdf:Description> |
227 * </rdf:Description> |
228 * </rdf:RDF> |
228 * </rdf:RDF> |
229 * </pre> |
229 * }</pre> |
230 * |
230 * |
231 * <p>The "rdf" prefix is declared only once, because the RDF Namespace |
231 * <p>The "rdf" prefix is declared only once, because the RDF Namespace |
232 * is used by the root element and can be inherited by all of its |
232 * is used by the root element and can be inherited by all of its |
233 * descendants; the "dc" prefix, on the other hand, is declared three |
233 * descendants; the "dc" prefix, on the other hand, is declared three |
234 * times, because no higher element uses the Namespace. To solve this |
234 * times, because no higher element uses the Namespace. To solve this |
241 * |
241 * |
242 * <p>Now, the "dc" prefix will be declared on the root element even |
242 * <p>Now, the "dc" prefix will be declared on the root element even |
243 * though it's not needed there, and can be inherited by its |
243 * though it's not needed there, and can be inherited by its |
244 * descendants:</p> |
244 * descendants:</p> |
245 * |
245 * |
246 * <pre> |
246 * <pre>{@code |
247 * <xml version="1.0" standalone="yes"?> |
247 * <xml version="1.0" standalone="yes"?> |
248 * |
248 * |
249 * <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" |
249 * <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" |
250 * xmlns:dc="http://www.purl.org/dc/"> |
250 * xmlns:dc="http://www.purl.org/dc/"> |
251 * <rdf:Description about="http://www.foo.com/ids/books/12345"> |
251 * <rdf:Description about="http://www.foo.com/ids/books/12345"> |
252 * <dc:title>A Dark Night</dc:title> |
252 * <dc:title>A Dark Night</dc:title> |
253 * <dc:creator>Jane Smith</dc:title> |
253 * <dc:creator>Jane Smith</dc:title> |
254 * <dc:date>2000-09-09</dc:title> |
254 * <dc:date>2000-09-09</dc:title> |
255 * </rdf:Description> |
255 * </rdf:Description> |
256 * </rdf:RDF> |
256 * </rdf:RDF> |
257 * </pre> |
257 * }</pre> |
258 * |
258 * |
259 * <p>This approach is also useful for declaring Namespace prefixes |
259 * <p>This approach is also useful for declaring Namespace prefixes |
260 * that be used by qualified names appearing in attribute values or |
260 * that be used by qualified names appearing in attribute values or |
261 * character data.</p> |
261 * character data.</p> |
262 * |
262 * |
386 this.encoding = encoding; |
386 this.encoding = encoding; |
387 } |
387 } |
388 |
388 |
389 /** |
389 /** |
390 * Set whether the writer should print out the XML declaration |
390 * Set whether the writer should print out the XML declaration |
391 * (<?xml version='1.0' ... ?>). |
391 * ({@code <?xml version='1.0' ... ?>}). |
392 * <p> |
392 * <p> |
393 * This option is set to true by default. |
393 * This option is set to true by default. |
394 */ |
394 */ |
395 public void setXmlDecl( boolean _writeXmlDecl ) { |
395 public void setXmlDecl( boolean _writeXmlDecl ) { |
396 this.writeXmlDecl = _writeXmlDecl; |
396 this.writeXmlDecl = _writeXmlDecl; |