Skip to main content

Element

Struct Element 

Source
pub struct Element {
    pub prefix: Option<String>,
    pub namespace: Option<String>,
    pub namespaces: Option<Namespace>,
    pub name: String,
    pub attributes: AttributeMap<String, String>,
    pub children: Vec<XMLNode>,
}
Expand description

Represents an XML element.

Fields§

§prefix: Option<String>

This elements prefix, if any

§namespace: Option<String>

This elements namespace, if any

§namespaces: Option<Namespace>

The full list of namespaces, if any

The Namespace type is exported from the xml-rs crate.

§name: String

The name of the Element. Does not include any namespace info

§attributes: AttributeMap<String, String>

The Element attributes

By default, this is a HashMap, but there are two optional features that can change this:

  • If the “attribute-order” feature is enabled, then this is an IndexMap, which will retain item insertion order.
  • If the “attribute-sorted” feature is enabled, then this is a std::collections::BTreeMap, which maintains keys in sorted order.
§children: Vec<XMLNode>

Children

Implementations§

Source§

impl Element

Source

pub fn new(name: &str) -> Element

Create a new empty element with given name

All other fields are empty

Source

pub fn parse_all<R: Read>(r: R) -> Result<Vec<XMLNode>, ParseError>

Parses some data into a list of XMLNodes

This is useful when you want to capture comments or processing instructions that appear before or after the root node

Source

pub fn parse_all_with_config<R: Read>( r: R, parser_config: ParserConfig, ) -> Result<Vec<XMLNode>, ParseError>

Source

pub fn parse<R: Read>(r: R) -> Result<Element, ParseError>

Parses some data into an Element

Source

pub fn parse_with_config<R: Read>( r: R, config: ParserConfig, ) -> Result<Element, ParseError>

Source

pub fn write<W: Write>(&self, w: W) -> Result<(), Error>

Writes out this element as the root element in an new XML document

Source

pub fn write_with_config<W: Write>( &self, w: W, config: EmitterConfig, ) -> Result<(), Error>

Writes out this element as the root element in a new XML document using the provided configuration

Source

pub fn get_child<P: ElementPredicate>(&self, k: P) -> Option<&Element>

Find a child element with the given name and return a reference to it.

Both &str and String implement ElementPredicate and can be used to search for child elements that match the given element name with .get_child("element_name"). You can also search by ("element_name", "tag_name") tuple.

Note: this will only return Elements. To get other nodes (like comments), iterate through the children field.

Source

pub fn get_mut_child<P: ElementPredicate>( &mut self, k: P, ) -> Option<&mut Element>

Find a child element with the given name and return a mutable reference to it.

Source

pub fn take_child<P: ElementPredicate>(&mut self, k: P) -> Option<Element>

Find a child element with the given name, remove and return it.

Source

pub fn get_text<'a>(&'a self) -> Option<Cow<'a, str>>

Returns the inner text/cdata of this element, if any.

If there are multiple text/cdata nodes, they will be all concatenated into one string.

Source

pub fn matches<P: ElementPredicate>(&self, k: P) -> bool

Checks if this element matches the predicate.

Trait Implementations§

Source§

impl Clone for Element

Source§

fn clone(&self) -> Element

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Element

Source§

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

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

impl Eq for Element

Source§

impl PartialEq for Element

Source§

fn eq(&self, other: &Element) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Element

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.