pub struct Document { /* private fields */ }Expand description
A Document represents a DOM plus additional (optional) metadata such as one or more Document Type Declarations (DTD). Use this struct to write a DOM to a string or file.
Implementations§
Source§impl Document
impl Document
Sourcepub fn new(root: Element) -> Self
pub fn new(root: Element) -> Self
Constructs a new Document with the given root element and default declaration
Sourcepub fn new_with_decl_dtd(
root: Element,
declaration: Option<Declaration>,
dtd: Option<&[DTD]>,
) -> Self
pub fn new_with_decl_dtd( root: Element, declaration: Option<Declaration>, dtd: Option<&[DTD]>, ) -> Self
Full constructor with required root element and optional XML declaration and optional list of one or more document type definition (DTD) items.
Sourcepub fn doctype_defs(&self) -> impl Iterator<Item = &DTD>
pub fn doctype_defs(&self) -> impl Iterator<Item = &DTD>
Returns a list of any and all DTDs for this Document as an iterator
Sourcepub fn doctype_defs_mut(&mut self) -> impl Iterator<Item = &mut DTD>
pub fn doctype_defs_mut(&mut self) -> impl Iterator<Item = &mut DTD>
Returns a list of any and all DTDs for this Document as an iterator
Sourcepub fn set_doctype_defs(&mut self, dtds: Option<&[DTD]>)
pub fn set_doctype_defs(&mut self, dtds: Option<&[DTD]>)
Sets the DTDs for this document (a None argument will remove all DTDs)
Sourcepub fn declaration(&self) -> &Option<Declaration>
pub fn declaration(&self) -> &Option<Declaration>
Gets the XML declaration for this document, if it has one (while the XML spec requires a declaration at the start of every XML file, it is commonly omitted, especially when the XML is embedded in a stream or file).
Sourcepub fn set_declaration(&mut self, decl: Option<Declaration>)
pub fn set_declaration(&mut self, decl: Option<Declaration>)
Sets the XML declaration for this document (a None argument will remove any existing declaration). While the XML spec requires a declaration at the start of every XML file, it is commonly omitted, especially when the XML is embedded in a stream or file.
Sourcepub fn to_string(&self) -> String
pub fn to_string(&self) -> String
Produces the XML text representing this XML DOM using the default indent of two spaces per level
Sourcepub fn to_string_with_indent(&self, indent: impl Into<String>) -> String
pub fn to_string_with_indent(&self, indent: impl Into<String>) -> String
Produces the XML text representing this XML DOM using the provided indent.
§Args:
- indent - prefix string to use for indenting the output XML. The indent must be either a single tab character or any number of spaces (otherwise a warning will be printed and the default indent used instead)
Sourcepub fn write_to_filepath(&self, path: impl AsRef<Path>) -> Result<()>
pub fn write_to_filepath(&self, path: impl AsRef<Path>) -> Result<()>
Writes this document as XML to the given file using the default indent of two spaces per level, returning a result indicating success or error in this write operation
Sourcepub fn write_to_filepath_with_indent(
&self,
path: impl AsRef<Path>,
indent: impl Into<String>,
) -> Result<()>
pub fn write_to_filepath_with_indent( &self, path: impl AsRef<Path>, indent: impl Into<String>, ) -> Result<()>
Writes this document as XML to the given file using the provided indent, returning a result indicating success or error in this write operation
Sourcepub fn write_to_file(&self, out: &mut impl Write) -> Result<()>
pub fn write_to_file(&self, out: &mut impl Write) -> Result<()>
Writes this document as XML to the given file or stream using the default indent of two spaces per level, returning a result indicating success or error in this write operation
Sourcepub fn write_to_file_with_indent(
&self,
out: &mut impl Write,
indent: impl Into<String>,
) -> Result<()>
pub fn write_to_file_with_indent( &self, out: &mut impl Write, indent: impl Into<String>, ) -> Result<()>
Writes this document as XML to the given file or stream using the default indent of two spaces per level, returning a result indicating success or error in this write operation
Sourcepub fn root_element(&self) -> &Element
pub fn root_element(&self) -> &Element
Returns the root element of this DOM as an immutable reference
Sourcepub fn root_element_mut(&mut self) -> &mut Element
pub fn root_element_mut(&mut self) -> &mut Element
Returns the root element of this DOM as a mutable reference.