Skip to main content

XmlPrinter

Struct XmlPrinter 

Source
pub struct XmlPrinter { /* private fields */ }
Expand description

An XML serialization and streaming engine.

Implements XmlVisitor to serialize an in-memory Document, and provides a stateful streaming API for manual XML generation without a DOM tree.

Implementations§

Source§

impl XmlPrinter

Source

pub fn new() -> Self

Creates a new pretty-printing printer.

The default indentation is 4 spaces.

Source

pub fn new_compact() -> Self

Creates a new compact printer.

Compact mode outputs XML with zero indentation or newlines between elements.

Source

pub fn compact() -> Self

Creates a new compact printer. Alias for Self::new_compact.

Source

pub fn with_capacity(capacity: usize, compact: bool) -> Self

Creates a printer with a pre-allocated capacity.

Source

pub fn set_bom(&mut self, use_bom: bool)

Configures whether to emit the UTF-8 Byte Order Mark (BOM).

Source

pub fn set_indent_str(&mut self, indent_str: &str)

Configures the indentation string used in pretty-printed mode.

Source

pub fn clear(&mut self)

Clears the printer’s state and buffer for reuse.

Source

pub fn as_str(&self) -> &str

Returns the accumulated XML output as a string slice.

Source

pub fn result(&self) -> &str

Returns the accumulated XML output as a string slice. Alias for Self::as_str.

Source

pub fn into_string(self) -> String

Consumes the printer and returns the accumulated XML string.

Source

pub fn len(&self) -> usize

Returns the current length of the output buffer in bytes.

Source

pub fn is_empty(&self) -> bool

Returns true if the output buffer is empty.

Source

pub fn push_header( &mut self, version: &str, encoding: Option<&str>, standalone: Option<bool>, )

Writes the XML declaration: <?xml version="..." encoding="..." standalone="..."?>

Source

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

Opens an XML element: <name (remains open for attributes).

Source

pub fn push_attribute(&mut self, name: &str, value: &str)

Pushes an attribute key-value pair to the currently open element.

§Panics

Panics if no element is currently open for attributes.

Source

pub fn close_element(&mut self)

Closes the currently open XML element: /> or </name>.

Source

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

Pushes entity-escaped text content.

Source

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

Pushes unescaped raw text content.

Source

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

Pushes a CDATA section: <![CDATA[text]]>.

Source

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

Pushes a comment: <!--text-->.

Source

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

Pushes a declaration block: <?text?>.

Source

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

Pushes an unknown block: <!text>.

Source

pub fn push_declaration_node(&mut self, name: &str, attrs: &[Attribute])

Pushes a declaration node with attributes: <?name attr="value"?>.

Trait Implementations§

Source§

impl Default for XmlPrinter

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl XmlVisitor for XmlPrinter

Source§

fn visit_enter_document(&mut self, doc: &Document) -> bool

Called when entering the Document (before any children).
Source§

fn visit_exit_document(&mut self, _doc: &Document) -> bool

Called when exiting the Document (after all children).
Source§

fn visit_enter_element(&mut self, doc: &Document, element: NodeId) -> bool

Called when entering an Element node (before children).
Source§

fn visit_exit_element(&mut self, _doc: &Document, _element: NodeId) -> bool

Called when leaving an Element node (after children).
Source§

fn visit_text(&mut self, doc: &Document, text: NodeId) -> bool

Called when visiting a Text node.
Source§

fn visit_comment(&mut self, doc: &Document, comment: NodeId) -> bool

Called when visiting a Comment node.
Source§

fn visit_declaration(&mut self, doc: &Document, declaration: NodeId) -> bool

Called when visiting a Declaration node.
Source§

fn visit_unknown(&mut self, doc: &Document, unknown: NodeId) -> bool

Called when visiting an Unknown node.

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.