Struct quick_xml::writer::ElementWriter
source · pub struct ElementWriter<'a, W> { /* private fields */ }
Expand description
A struct to write an element. Contains methods to add attributes and inner elements to the element
Implementations§
source§impl<'a, W: AsyncWrite + Unpin> ElementWriter<'a, W>
impl<'a, W: AsyncWrite + Unpin> ElementWriter<'a, W>
sourcepub async fn write_text_content_async(
self,
text: BytesText<'_>
) -> Result<&'a mut Writer<W>>
Available on crate feature async-tokio
only.
pub async fn write_text_content_async( self, text: BytesText<'_> ) -> Result<&'a mut Writer<W>>
async-tokio
only.Write some text inside the current element.
Example
let mut buffer = Vec::new();
let mut tokio_buffer = tokio::io::BufWriter::new(&mut buffer);
let mut writer = Writer::new_with_indent(&mut tokio_buffer, b' ', 4);
writer
.create_element("paired")
.with_attribute(("attr1", "value1"))
.with_attribute(("attr2", "value2"))
.write_text_content_async(BytesText::new("text"))
.await
.expect("cannot write content");
tokio_buffer.flush().await.expect("flush failed");
assert_eq!(
std::str::from_utf8(&buffer).unwrap(),
r#"<paired attr1="value1" attr2="value2">text</paired>"#
);
sourcepub async fn write_cdata_content_async(
self,
text: BytesCData<'_>
) -> Result<&'a mut Writer<W>>
Available on crate feature async-tokio
only.
pub async fn write_cdata_content_async( self, text: BytesCData<'_> ) -> Result<&'a mut Writer<W>>
async-tokio
only.Write a CData event <![CDATA[...]]>
inside the current element.
Example
let mut buffer = Vec::new();
let mut tokio_buffer = tokio::io::BufWriter::new(&mut buffer);
let mut writer = Writer::new_with_indent(&mut tokio_buffer, b' ', 4);
writer
.create_element("paired")
.with_attribute(("attr1", "value1"))
.with_attribute(("attr2", "value2"))
.write_cdata_content_async(BytesCData::new("text & content"))
.await
.expect("cannot write content");
tokio_buffer.flush().await.expect("flush failed");
assert_eq!(
std::str::from_utf8(&buffer).unwrap(),
r#"<paired attr1="value1" attr2="value2"><![CDATA[text & content]]></paired>"#
);
sourcepub async fn write_pi_content_async(
self,
text: BytesText<'_>
) -> Result<&'a mut Writer<W>>
Available on crate feature async-tokio
only.
pub async fn write_pi_content_async( self, text: BytesText<'_> ) -> Result<&'a mut Writer<W>>
async-tokio
only.Write a processing instruction <?...?>
inside the current element.
Example
let mut buffer = Vec::new();
let mut tokio_buffer = tokio::io::BufWriter::new(&mut buffer);
let mut writer = Writer::new_with_indent(&mut tokio_buffer, b' ', 4);
writer
.create_element("paired")
.with_attribute(("attr1", "value1"))
.with_attribute(("attr2", "value2"))
// NOTE: We cannot use BytesText::new here, because it escapes strings,
// but processing instruction content should not be escaped
.write_pi_content_async(BytesText::from_escaped(r#"xml-stylesheet href="style.css""#))
.await
.expect("cannot write content");
tokio_buffer.flush().await.expect("flush failed");
assert_eq!(
std::str::from_utf8(&buffer).unwrap(),
r#"<paired attr1="value1" attr2="value2">
<?xml-stylesheet href="style.css"?>
</paired>"#
);
sourcepub async fn write_empty_async(self) -> Result<&'a mut Writer<W>>
Available on crate feature async-tokio
only.
pub async fn write_empty_async(self) -> Result<&'a mut Writer<W>>
async-tokio
only.Write an empty (self-closing) tag.
Example
let mut buffer = Vec::new();
let mut tokio_buffer = tokio::io::BufWriter::new(&mut buffer);
let mut writer = Writer::new_with_indent(&mut tokio_buffer, b' ', 4);
writer
.create_element("empty")
.with_attribute(("attr1", "value1"))
.with_attribute(("attr2", "value2"))
.write_empty_async()
.await
.expect("cannot write content");
tokio_buffer.flush().await.expect("flush failed");
assert_eq!(
std::str::from_utf8(&buffer).unwrap(),
r#"<empty attr1="value1" attr2="value2"/>"#
);
sourcepub async fn write_inner_content_async<F, Fut, E>(
self,
closure: F
) -> StdResult<&'a mut Writer<W>, E>where
F: FnOnce(&'a mut Writer<W>) -> Fut,
Fut: Future<Output = StdResult<&'a mut Writer<W>, E>>,
E: From<Error>,
Available on crate feature async-tokio
only.
pub async fn write_inner_content_async<F, Fut, E>( self, closure: F ) -> StdResult<&'a mut Writer<W>, E>where F: FnOnce(&'a mut Writer<W>) -> Fut, Fut: Future<Output = StdResult<&'a mut Writer<W>, E>>, E: From<Error>,
async-tokio
only.Create a new scope for writing XML inside the current element.
Example
use quick_xml::Error;
let mut buffer = Vec::new();
let mut tokio_buffer = tokio::io::BufWriter::new(&mut buffer);
let mut writer = Writer::new_with_indent(&mut tokio_buffer, b' ', 4);
writer
.create_element("outer")
.with_attributes([("attr1", "value1"), ("attr2", "value2")])
// We need to provide error type, because it is not named somewhere explicitly
.write_inner_content_async::<_, _, Error>(|writer| async move {
let fruits = ["apple", "orange", "banana"];
for (quant, item) in fruits.iter().enumerate() {
writer
.create_element("fruit")
.with_attributes([("quantity", quant.to_string().as_str())])
.write_text_content_async(BytesText::new(item))
.await?;
}
writer
.create_element("inner")
.write_inner_content_async(|writer| async move {
writer.create_element("empty").write_empty_async().await
})
.await?;
Ok(writer)
})
.await
.expect("cannot write content");
tokio_buffer.flush().await.expect("flush failed");
assert_eq!(
std::str::from_utf8(&buffer).unwrap(),
r#"<outer attr1="value1" attr2="value2">
<fruit quantity="0">apple</fruit>
<fruit quantity="1">orange</fruit>
<fruit quantity="2">banana</fruit>
<inner>
<empty/>
</inner>
</outer>"#
);
source§impl<'a, W> ElementWriter<'a, W>
impl<'a, W> ElementWriter<'a, W>
sourcepub fn with_attribute<'b, I>(self, attr: I) -> Selfwhere
I: Into<Attribute<'b>>,
pub fn with_attribute<'b, I>(self, attr: I) -> Selfwhere I: Into<Attribute<'b>>,
Adds an attribute to this element.
sourcepub fn with_attributes<'b, I>(self, attributes: I) -> Selfwhere
I: IntoIterator,
I::Item: Into<Attribute<'b>>,
pub fn with_attributes<'b, I>(self, attributes: I) -> Selfwhere I: IntoIterator, I::Item: Into<Attribute<'b>>,
Add additional attributes to this element using an iterator.
The yielded items must be convertible to Attribute
using Into
.
source§impl<'a, W: Write> ElementWriter<'a, W>
impl<'a, W: Write> ElementWriter<'a, W>
sourcepub fn write_text_content(
self,
text: BytesText<'_>
) -> Result<&'a mut Writer<W>>
pub fn write_text_content( self, text: BytesText<'_> ) -> Result<&'a mut Writer<W>>
Write some text inside the current element.
sourcepub fn write_cdata_content(
self,
text: BytesCData<'_>
) -> Result<&'a mut Writer<W>>
pub fn write_cdata_content( self, text: BytesCData<'_> ) -> Result<&'a mut Writer<W>>
Write a CData event <![CDATA[...]]>
inside the current element.
sourcepub fn write_pi_content(self, text: BytesText<'_>) -> Result<&'a mut Writer<W>>
pub fn write_pi_content(self, text: BytesText<'_>) -> Result<&'a mut Writer<W>>
Write a processing instruction <?...?>
inside the current element.
sourcepub fn write_empty(self) -> Result<&'a mut Writer<W>>
pub fn write_empty(self) -> Result<&'a mut Writer<W>>
Write an empty (self-closing) tag.
Auto Trait Implementations§
impl<'a, W> RefUnwindSafe for ElementWriter<'a, W>where W: RefUnwindSafe,
impl<'a, W> Send for ElementWriter<'a, W>where W: Send,
impl<'a, W> Sync for ElementWriter<'a, W>where W: Sync,
impl<'a, W> Unpin for ElementWriter<'a, W>
impl<'a, W> !UnwindSafe for ElementWriter<'a, W>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more