pub struct XmlWriter { /* private fields */ }
Expand description
An XML writer.
Implementations§
source§impl XmlWriter
impl XmlWriter
sourcepub fn with_capacity(capacity: usize, opt: Options) -> Self
pub fn with_capacity(capacity: usize, opt: Options) -> Self
Creates a new XmlWriter
with a specified capacity.
sourcepub fn write_declaration(&mut self)
pub fn write_declaration(&mut self)
Writes an XML declaration.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
§Panics
- When called twice.
sourcepub fn write_comment(&mut self, text: &str)
pub fn write_comment(&mut self, text: &str)
Writes a comment string.
sourcepub fn write_comment_fmt(&mut self, fmt: Arguments<'_>)
pub fn write_comment_fmt(&mut self, fmt: Arguments<'_>)
Writes a formatted comment.
sourcepub fn start_element(&mut self, name: &str)
pub fn start_element(&mut self, name: &str)
Starts writing a new element.
This method writes only the <tag-name
part.
sourcepub fn write_attribute<V: Display + ?Sized>(&mut self, name: &str, value: &V)
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");
sourcepub fn write_attribute_fmt(&mut self, name: &str, fmt: Arguments<'_>)
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");
sourcepub fn write_attribute_raw<F>(&mut self, name: &str, f: F)
pub fn write_attribute_raw<F>(&mut self, name: &str, f: F)
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");
sourcepub fn set_preserve_whitespaces(&mut self, preserve: bool)
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>
");
sourcepub fn write_text(&mut self, text: &str)
pub fn write_text(&mut self, text: &str)
Writes a text node.
See write_text_fmt()
for details.
sourcepub fn write_text_fmt(&mut self, fmt: Arguments<'_>)
pub fn write_text_fmt(&mut self, fmt: Arguments<'_>)
sourcepub fn end_element(&mut self)
pub fn end_element(&mut self)
Closes an open element.
sourcepub fn end_document(self) -> String
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>
");