Struct quick_xml::writer::Writer
[−]
[src]
pub struct Writer<W: Write> { /* fields omitted */ }
Xml writer
Consumes a Write
and writes xml Events
use quick_xml::writer::Writer; use quick_xml::events::{Event, BytesEnd, BytesStart}; use quick_xml::reader::Reader; use std::io::Cursor; use std::iter; let xml = r#"<this_tag k1="v1" k2="v2"><child>text</child></this_tag>"#; let mut reader = Reader::from_str(xml); reader.trim_text(true); 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.with_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 assert!(writer.write_event(Event::Start(elem)).is_ok()); }, Ok(Event::End(ref e)) if e.name() == b"this_tag" => { assert!(writer.write_event(Event::End(BytesEnd::borrowed(b"my_elem"))).is_ok()); }, Ok(Event::Eof) => break, Ok(e) => assert!(writer.write_event(e).is_ok()), // or using the buffer // Ok(e) => assert!(writer.write(&buf).is_ok()), // error are chained, the last one usually being the // position where the error has happened Err(e) => panic!("{:?}", e.iter().map(|e| format!("{:?} -", e)).collect::<String>()), } buf.clear(); } 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());
Methods
impl<W: Write> Writer<W>
[src]
fn new(inner: W) -> Writer<W>
Creates a Writer from a generic Write
fn into_inner(self) -> W
Consumes this Writer
, returning the underlying writer.
fn write_event(&mut self, event: Event) -> Result<usize>
Writes the given event to the underlying writer.
fn write(&mut self, value: &[u8]) -> Result<usize>
Writes bytes
Trait Implementations
impl<W: Clone + Write> Clone for Writer<W>
[src]
fn clone(&self) -> Writer<W>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more