Struct Document

Source
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

Source

pub fn new() -> Document

Create a blank new xml document.

Source

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.

Source

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.

Source

pub fn root_nodes(&self) -> &Vec<Node>

Get root nodes of document.

Source

pub fn root_element(&self) -> Option<Element>

Get first root node that is an element.

Source

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

 

§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

Source

pub fn parse_str(str: &str) -> Result<Document>

Source

pub fn parse_str_with_opts(str: &str, opts: ReadOptions) -> Result<Document>

Source

pub fn parse_file<P: AsRef<Path>>(path: P) -> Result<Document>

Source

pub fn parse_file_with_opts<P: AsRef<Path>>( path: P, opts: ReadOptions, ) -> Result<Document>

Source

pub fn parse_reader<R: Read>(reader: R) -> Result<Document>

Source

pub fn parse_reader_with_opts<R: Read>( reader: R, opts: ReadOptions, ) -> Result<Document>

Source§

impl Document

 

§Writing

Below are methods for writing xml. The XML will be written in UTF-8.

Source

pub fn write_file<P: AsRef<Path>>(&self, path: P) -> Result<()>

Source

pub fn write_file_with_opts<P: AsRef<Path>>( &self, path: P, opts: WriteOptions, ) -> Result<()>

Source

pub fn write_str(&self) -> Result<String>

Source

pub fn write_str_with_opts(&self, opts: WriteOptions) -> Result<String>

Source

pub fn write(&self, writer: &mut impl Write) -> Result<()>

Source

pub fn write_with_opts( &self, writer: &mut impl Write, opts: WriteOptions, ) -> Result<()>

Trait Implementations§

Source§

impl Debug for Document

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FromStr for Document

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Document>

Parses a string s to return a value of this type. Read more

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.