Struct Document
pub struct Document<'input> { /* private fields */ }Expand description
An XML tree container.
A tree consists of Nodes.
There are no separate structs for each node type.
So you should check the current node type yourself via Node::node_type().
There are only 5 types:
Root, Element, PI, Comment and Text.
As you can see there are no XML declaration and CDATA types. The XML declaration is basically skipped, since it doesn’t contain any valuable information (we support only UTF-8 anyway). And CDATA will be converted into a Text node as is, without any preprocessing (you can read more about it here).
Also, the Text node data can be accessed from the text node itself or from
the parent element via Node::text() or Node::tail().
Implementations§
§impl<'input> Document<'input>
impl<'input> Document<'input>
pub fn parse(text: &'input str) -> Result<Document<'input>, Error>
pub fn parse(text: &'input str) -> Result<Document<'input>, Error>
Parses the input XML string.
We do not support &[u8] or Reader because the input must be an already allocated
UTF-8 string.
This is a shorthand for Document::parse_with_options(data, ParsingOptions::default()).
§Examples
let doc = roxmltree::Document::parse("<e/>").unwrap();
assert_eq!(doc.descendants().count(), 2); // root node + `e` element nodepub fn parse_with_options(
text: &'input str,
opt: ParsingOptions<'input>,
) -> Result<Document<'input>, Error>
pub fn parse_with_options( text: &'input str, opt: ParsingOptions<'input>, ) -> Result<Document<'input>, Error>
Parses the input XML string using to selected options.
We do not support &[u8] or Reader because the input must be an already allocated
UTF-8 string.
§Examples
let opt = roxmltree::ParsingOptions::default();
let doc = roxmltree::Document::parse_with_options("<e/>", opt).unwrap();
assert_eq!(doc.descendants().count(), 2); // root node + `e` element node§impl<'input> Document<'input>
impl<'input> Document<'input>
pub fn root<'a>(&'a self) -> Node<'a, 'input>
pub fn root<'a>(&'a self) -> Node<'a, 'input>
Returns the root node.
§Examples
let doc = roxmltree::Document::parse("<e/>").unwrap();
assert!(doc.root().is_root());
assert!(doc.root().first_child().unwrap().has_tag_name("e"));pub fn get_node<'a>(&'a self, id: NodeId) -> Option<Node<'a, 'input>>
pub fn get_node<'a>(&'a self, id: NodeId) -> Option<Node<'a, 'input>>
Returns the node of the tree with the given NodeId.
Note: NodeId::new(0) represents the root node
§Examples
let doc = roxmltree::Document::parse("\
<p>
text
</p>
").unwrap();
use roxmltree::NodeId;
assert_eq!(doc.get_node(NodeId::new(0)).unwrap(), doc.root());
assert_eq!(doc.get_node(NodeId::new(1)), doc.descendants().find(|n| n.has_tag_name("p")));
assert_eq!(doc.get_node(NodeId::new(2)), doc.descendants().find(|n| n.is_text()));
assert_eq!(doc.get_node(NodeId::new(3)), None);pub fn root_element<'a>(&'a self) -> Node<'a, 'input>
pub fn root_element<'a>(&'a self) -> Node<'a, 'input>
Returns the root element of the document.
Unlike root, will return a first element node.
The root element always exists.
§Examples
let doc = roxmltree::Document::parse("<!-- comment --><e/>").unwrap();
assert!(doc.root_element().has_tag_name("e"));pub fn descendants(&self) -> Descendants<'_, 'input>
pub fn descendants(&self) -> Descendants<'_, 'input>
Returns an iterator over document’s descendant nodes.
Shorthand for doc.root().descendants().
pub fn text_pos_at(&self, pos: usize) -> TextPos
pub fn text_pos_at(&self, pos: usize) -> TextPos
Calculates TextPos in the original document from position in bytes.
Note: this operation is expensive.
§Examples
use roxmltree::*;
let doc = Document::parse("\
<!-- comment -->
<e/>"
).unwrap();
assert_eq!(doc.text_pos_at(10), TextPos::new(1, 11));
assert_eq!(doc.text_pos_at(9999), TextPos::new(2, 5));pub fn input_text(&self) -> &'input str
pub fn input_text(&self) -> &'input str
Returns the input text of the original document.
§Examples
use roxmltree::*;
let doc = Document::parse("<e/>").unwrap();
assert_eq!(doc.input_text(), "<e/>");Trait Implementations§
Auto Trait Implementations§
impl<'input> Freeze for Document<'input>
impl<'input> RefUnwindSafe for Document<'input>
impl<'input> Send for Document<'input>
impl<'input> Sync for Document<'input>
impl<'input> Unpin for Document<'input>
impl<'input> UnwindSafe for Document<'input>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<Source, Target> OctetsInto<Target> for Sourcewhere
Target: OctetsFrom<Source>,
impl<Source, Target> OctetsInto<Target> for Sourcewhere
Target: OctetsFrom<Source>,
type Error = <Target as OctetsFrom<Source>>::Error
§fn try_octets_into(
self,
) -> Result<Target, <Source as OctetsInto<Target>>::Error>
fn try_octets_into( self, ) -> Result<Target, <Source as OctetsInto<Target>>::Error>
§fn octets_into(self) -> Targetwhere
Self::Error: Into<Infallible>,
fn octets_into(self) -> Targetwhere
Self::Error: Into<Infallible>,
§impl<D> OwoColorize for D
impl<D> OwoColorize for D
§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg] or
a color-specific method, such as [OwoColorize::green], Read more§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg] or
a color-specific method, such as [OwoColorize::on_yellow], Read more