pub struct Document { /* private fields */ }
Expand description
Represents a XML document or a document fragment.
To build a document from scratch, use Document::new
.
To read and modify an existing document, use parse_* methods.
To write the document, use write_* methods.
§Examples
use xml_doc::Document;
let mut doc = Document::parse_str(r#"<?xml version="1.0" encoding="UTF-8"?>
<package>
<metadata>
<author>Lewis Carol</author>
</metadata>
</package>
"#).unwrap();
let author_elem = doc
.root_element()
.unwrap()
.find(&doc, "metadata")
.unwrap()
.find(&doc, "author")
.unwrap();
author_elem.set_text_content(&mut doc, "Lewis Carroll");
let xml = doc.write_str();
Implementations§
Source§impl Document
impl Document
Sourcepub fn container(&self) -> Element
pub fn container(&self) -> Element
Get ‘container’ element of Document.
The document uses an invisible ‘container’ element which it uses to manage its root nodes.
Its parent is None, and trying to change its parent will
return Error::ContainerCannotMove
.
For the container element, only its children
is relevant.
Other attributes are not used.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if document doesn’t have any nodes.
Returns false
if you added a node or parsed an xml.
You can only call parse_*()
if document is empty.
Sourcepub fn root_nodes(&self) -> &Vec<Node>
pub fn root_nodes(&self) -> &Vec<Node>
Get root nodes of document.
Sourcepub fn root_element(&self) -> Option<Element>
pub fn root_element(&self) -> Option<Element>
Get first root node that is an element.
Sourcepub fn push_root_node(&mut self, node: Node) -> Result<()>
pub fn push_root_node(&mut self, node: Node) -> Result<()>
Push a node to end of root nodes.
If doc has no Element
, pushing a Node::Element
is
equivalent to setting it as root element.
Source§impl Document
impl Document
§Parsing
Below are methods for parsing xml. Parsing from string, file, and reader is supported.
Call parse_*_with_opts
with custom ReadOptions
to change parser behaviour.
Otherwise, ReadOptions::default()
is used.
§Errors
Error::CannotDecode
: Could not decode XML. XML declaration may have invalid encoding value.Error::MalformedXML
: Could not read XML.Error::Io
: IO Error
pub fn parse_str(str: &str) -> Result<Document>
pub fn parse_str_with_opts(str: &str, opts: ReadOptions) -> Result<Document>
pub fn parse_file<P: AsRef<Path>>(path: P) -> Result<Document>
pub fn parse_file_with_opts<P: AsRef<Path>>( path: P, opts: ReadOptions, ) -> Result<Document>
pub fn parse_reader<R: Read>(reader: R) -> Result<Document>
pub fn parse_reader_with_opts<R: Read>( reader: R, opts: ReadOptions, ) -> Result<Document>
Source§impl Document
impl Document
§Writing
Below are methods for writing xml. The XML will be written in UTF-8.