@@ -97,6 +97,59 @@ impl<W> Writer<W> {
97
97
pub fn get_ref ( & self ) -> & W {
98
98
& self . writer
99
99
}
100
+
101
+ /// Provides a simple, high-level API for writing XML elements.
102
+ ///
103
+ /// Returns an [`ElementWriter`] that simplifies setting attributes and writing
104
+ /// content inside the element.
105
+ ///
106
+ /// # Example
107
+ ///
108
+ /// ```rust
109
+ /// # use quick_xml::Result;
110
+ /// # fn main() -> Result<()> {
111
+ /// use quick_xml::events::{BytesStart, BytesText, Event};
112
+ /// use quick_xml::writer::Writer;
113
+ /// use quick_xml::Error;
114
+ /// use std::io::Cursor;
115
+ ///
116
+ /// let mut writer = Writer::new(Cursor::new(Vec::new()));
117
+ ///
118
+ /// // writes <tag attr1="value1"/>
119
+ /// writer.create_element("tag")
120
+ /// .with_attribute(("attr1", "value1")) // chain `with_attribute()` calls to add many attributes
121
+ /// .write_empty()?;
122
+ ///
123
+ /// // writes <tag attr1="value1" attr2="value2">with some text inside</tag>
124
+ /// writer.create_element("tag")
125
+ /// .with_attributes(vec![("attr1", "value1"), ("attr2", "value2")].into_iter()) // or add attributes from an iterator
126
+ /// .write_text_content(BytesText::new("with some text inside"))?;
127
+ ///
128
+ /// // writes <tag><fruit quantity="0">apple</fruit><fruit quantity="1">orange</fruit></tag>
129
+ /// writer.create_element("tag")
130
+ /// .write_inner_content(|writer| {
131
+ /// let fruits = ["apple", "orange"];
132
+ /// for (quant, item) in fruits.iter().enumerate() {
133
+ /// writer
134
+ /// .create_element("fruit")
135
+ /// .with_attribute(("quantity", quant.to_string().as_str()))
136
+ /// .write_text_content(BytesText::new(item))?;
137
+ /// }
138
+ /// Ok(())
139
+ /// })?;
140
+ /// # Ok(())
141
+ /// # }
142
+ /// ```
143
+ #[ must_use]
144
+ pub fn create_element < ' a , N > ( & ' a mut self , name : & ' a N ) -> ElementWriter < W >
145
+ where
146
+ N : ' a + AsRef < str > + ?Sized ,
147
+ {
148
+ ElementWriter {
149
+ writer : self ,
150
+ start_tag : BytesStart :: new ( name. as_ref ( ) ) ,
151
+ }
152
+ }
100
153
}
101
154
102
155
impl < W : Write > Writer < W > {
@@ -213,59 +266,6 @@ impl<W: Write> Writer<W> {
213
266
Ok ( ( ) )
214
267
}
215
268
216
- /// Provides a simple, high-level API for writing XML elements.
217
- ///
218
- /// Returns an [`ElementWriter`] that simplifies setting attributes and writing
219
- /// content inside the element.
220
- ///
221
- /// # Example
222
- ///
223
- /// ```rust
224
- /// # use quick_xml::Result;
225
- /// # fn main() -> Result<()> {
226
- /// use quick_xml::events::{BytesStart, BytesText, Event};
227
- /// use quick_xml::writer::Writer;
228
- /// use quick_xml::Error;
229
- /// use std::io::Cursor;
230
- ///
231
- /// let mut writer = Writer::new(Cursor::new(Vec::new()));
232
- ///
233
- /// // writes <tag attr1="value1"/>
234
- /// writer.create_element("tag")
235
- /// .with_attribute(("attr1", "value1")) // chain `with_attribute()` calls to add many attributes
236
- /// .write_empty()?;
237
- ///
238
- /// // writes <tag attr1="value1" attr2="value2">with some text inside</tag>
239
- /// writer.create_element("tag")
240
- /// .with_attributes(vec![("attr1", "value1"), ("attr2", "value2")].into_iter()) // or add attributes from an iterator
241
- /// .write_text_content(BytesText::new("with some text inside"))?;
242
- ///
243
- /// // writes <tag><fruit quantity="0">apple</fruit><fruit quantity="1">orange</fruit></tag>
244
- /// writer.create_element("tag")
245
- /// .write_inner_content(|writer| {
246
- /// let fruits = ["apple", "orange"];
247
- /// for (quant, item) in fruits.iter().enumerate() {
248
- /// writer
249
- /// .create_element("fruit")
250
- /// .with_attribute(("quantity", quant.to_string().as_str()))
251
- /// .write_text_content(BytesText::new(item))?;
252
- /// }
253
- /// Ok(())
254
- /// })?;
255
- /// # Ok(())
256
- /// # }
257
- /// ```
258
- #[ must_use]
259
- pub fn create_element < ' a , N > ( & ' a mut self , name : & ' a N ) -> ElementWriter < W >
260
- where
261
- N : ' a + AsRef < str > + ?Sized ,
262
- {
263
- ElementWriter {
264
- writer : self ,
265
- start_tag : BytesStart :: new ( name. as_ref ( ) ) ,
266
- }
267
- }
268
-
269
269
/// Write an arbitrary serializable type
270
270
///
271
271
/// Note: If you are attempting to write XML in a non-UTF-8 encoding, this may not
@@ -335,12 +335,12 @@ impl<W: Write> Writer<W> {
335
335
336
336
/// A struct to write an element. Contains methods to add attributes and inner
337
337
/// elements to the element
338
- pub struct ElementWriter < ' a , W : Write > {
338
+ pub struct ElementWriter < ' a , W > {
339
339
writer : & ' a mut Writer < W > ,
340
340
start_tag : BytesStart < ' a > ,
341
341
}
342
342
343
- impl < ' a , W : Write > ElementWriter < ' a , W > {
343
+ impl < ' a , W > ElementWriter < ' a , W > {
344
344
/// Adds an attribute to this element.
345
345
pub fn with_attribute < ' b , I > ( mut self , attr : I ) -> Self
346
346
where
@@ -361,7 +361,9 @@ impl<'a, W: Write> ElementWriter<'a, W> {
361
361
self . start_tag . extend_attributes ( attributes) ;
362
362
self
363
363
}
364
+ }
364
365
366
+ impl < ' a , W : Write > ElementWriter < ' a , W > {
365
367
/// Write some text inside the current element.
366
368
pub fn write_text_content ( self , text : BytesText ) -> Result < & ' a mut Writer < W > > {
367
369
self . writer
0 commit comments