xmlwriter

Struct XmlWriter

source
pub struct XmlWriter { /* private fields */ }
Expand description

An XML writer.

Implementations§

source§

impl XmlWriter

source

pub fn new(opt: Options) -> Self

Creates a new XmlWriter.

source

pub fn with_capacity(capacity: usize, opt: Options) -> Self

Creates a new XmlWriter with a specified capacity.

source

pub fn write_declaration(&mut self)

Writes an XML declaration.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

§Panics
  • When called twice.
source

pub fn write_comment(&mut self, text: &str)

Writes a comment string.

source

pub fn write_comment_fmt(&mut self, fmt: Arguments<'_>)

Writes a formatted comment.

source

pub fn start_element(&mut self, name: &str)

Starts writing a new element.

This method writes only the <tag-name part.

source

pub fn write_attribute<V: Display + ?Sized>(&mut self, name: &str, value: &V)

Writes an attribute.

Quotes in the value will be escaped.

§Panics
  • When called before start_element().
  • When called after close_element().
§Example
use xmlwriter::*;

let mut w = XmlWriter::new(Options::default());
w.start_element("svg");
w.write_attribute("x", "5");
w.write_attribute("y", &5);
assert_eq!(w.end_document(), "<svg x=\"5\" y=\"5\"/>\n");
source

pub fn write_attribute_fmt(&mut self, name: &str, fmt: Arguments<'_>)

Writes a formatted attribute value.

Quotes in the value will be escaped.

§Panics
  • When called before start_element().
  • When called after close_element().
§Example
use xmlwriter::*;

let mut w = XmlWriter::new(Options::default());
w.start_element("rect");
w.write_attribute_fmt("fill", format_args!("url(#{})", "gradient"));
assert_eq!(w.end_document(), "<rect fill=\"url(#gradient)\"/>\n");
source

pub fn write_attribute_raw<F>(&mut self, name: &str, f: F)
where F: FnOnce(&mut Vec<u8>),

Writes a raw attribute value.

Closure provides a mutable reference to an internal buffer.

Warning: this method is an escape hatch for cases when you need to write a lot of data very fast.

§Panics
  • When called before start_element().
  • When called after close_element().
§Example
use xmlwriter::*;

let mut w = XmlWriter::new(Options::default());
w.start_element("path");
w.write_attribute_raw("d", |buf| buf.extend_from_slice(b"M 10 20 L 30 40"));
assert_eq!(w.end_document(), "<path d=\"M 10 20 L 30 40\"/>\n");
source

pub fn set_preserve_whitespaces(&mut self, preserve: bool)

Sets the preserve whitespaces flag.

  • If set, text nodes will be written as is.
  • If not set, text nodes will be indented.

Can be set at any moment.

§Example
use xmlwriter::*;

let mut w = XmlWriter::new(Options::default());
w.start_element("html");
w.start_element("p");
w.write_text("text");
w.end_element();
w.start_element("p");
w.set_preserve_whitespaces(true);
w.write_text("text");
w.end_element();
w.set_preserve_whitespaces(false);
assert_eq!(w.end_document(),
"<html>
    <p>
        text
    </p>
    <p>text</p>
</html>
");
source

pub fn write_text(&mut self, text: &str)

Writes a text node.

See write_text_fmt() for details.

source

pub fn write_text_fmt(&mut self, fmt: Arguments<'_>)

Writes a formatted text node.

< will be escaped.

§Panics
  • When called not after start_element().
source

pub fn end_element(&mut self)

Closes an open element.

source

pub fn end_document(self) -> String

Closes all open elements and returns an internal XML buffer.

§Example
use xmlwriter::*;

let mut w = XmlWriter::new(Options::default());
w.start_element("svg");
w.start_element("g");
w.start_element("rect");
assert_eq!(w.end_document(),
"<svg>
    <g>
        <rect/>
    </g>
</svg>
");

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

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

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

source§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.