[][src]Struct quick_xml::Writer

pub struct Writer<W: Write> { /* fields omitted */ }

XML writer.

Writes XML Events to a Write implementor.


use quick_xml::{Reader, Writer};
use quick_xml::events::{Event, BytesEnd, BytesStart};
use std::io::Cursor;

let xml = r#"<this_tag k1="v1" k2="v2"><child>text</child></this_tag>"#;
let mut reader = Reader::from_str(xml);
let mut writer = Writer::new(Cursor::new(Vec::new()));
let mut buf = Vec::new();
loop {
    match reader.read_event(&mut buf) {
        Ok(Event::Start(ref e)) if e.name() == b"this_tag" => {

            // crates a new element ... alternatively we could reuse `e` by calling
            // `e.into_owned()`
            let mut elem = BytesStart::owned(b"my_elem".to_vec(), "my_elem".len());

            // collect existing attributes
            elem.extend_attributes(e.attributes().map(|attr| attr.unwrap()));

            // copy existing attributes, adds a new my-key="some value" attribute
            elem.push_attribute(("my-key", "some value"));

            // writes the event to the writer
        Ok(Event::End(ref e)) if e.name() == b"this_tag" => {
        Ok(Event::Eof) => break,
        // we can either move or borrow the event to write, depending on your use-case
        Ok(e) => assert!(writer.write_event(&e).is_ok()),
        Err(e) => panic!("{}", e),

let result = writer.into_inner().into_inner();
let expected = r#"<my_elem k1="v1" k2="v2" my-key="some value"><child>text</child></my_elem>"#;
assert_eq!(result, expected.as_bytes());


impl<W: Write> Writer<W>[src]

pub fn new(inner: W) -> Writer<W>[src]

Creates a Writer from a generic Write

pub fn new_with_indent(
    inner: W,
    indent_char: u8,
    indent_size: usize
) -> Writer<W>

Creates a Writer with configured whitespace indents from a generic Write

pub fn into_inner(self) -> W[src]

Consumes this Writer, returning the underlying writer.

pub fn inner(&mut self) -> &mut W[src]

Get inner writer, keeping ownership

pub fn write_event<'a, E: AsRef<Event<'a>>>(&mut self, event: E) -> Result<()>[src]

Writes the given event to the underlying writer.

pub fn write(&mut self, value: &[u8]) -> Result<()>[src]

Writes bytes

pub fn write_indent(&mut self) -> Result<()>[src]

Manually write a newline and indentation at the proper level.

This can be used when the heuristic to line break and indent after any Event apart from Text fails such as when a Start occurs directly after Text. This method will do nothing if Writer was not constructed with new_with_indent.

Trait Implementations

impl<W: Clone + Write> Clone for Writer<W>[src]

Auto Trait Implementations

impl<W> RefUnwindSafe for Writer<W> where
    W: RefUnwindSafe

impl<W> Send for Writer<W> where
    W: Send

impl<W> Sync for Writer<W> where
    W: Sync

impl<W> Unpin for Writer<W> where
    W: Unpin

impl<W> UnwindSafe for Writer<W> where
    W: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized

impl<T> Borrow<T> for T where
    T: ?Sized

impl<T> BorrowMut<T> for T where
    T: ?Sized

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 

impl<T> ToOwned for T where
    T: Clone

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.