pub struct Element<'a, B: BufRead> { /* private fields */ }
Expand description
Represents a matched element while processing XML data.
Using this type we can enforce proper nesting when handling a stream of XML events with the help of the rust type system.
Implementations§
source§impl<B: BufRead> Element<'_, B>
impl<B: BufRead> Element<'_, B>
sourcepub fn name(&self) -> Cow<'_, str>
pub fn name(&self) -> Cow<'_, str>
Returns the name of the element.
§Example
let mut reader = PullReader::new("<xml></xml>".as_bytes());
assert_eq!(reader.root().unwrap().name().as_ref(), "xml");
sourcepub fn attributes(&self) -> AttributesView<'_> ⓘ
pub fn attributes(&self) -> AttributesView<'_> ⓘ
Provides access to the attributes of the element.
§Example
let mut reader = PullReader::new(r#"<xml test="foo"></xml>"#.as_bytes());
let root = reader.root().unwrap();
let attr = root.attributes().last().unwrap().unwrap();
assert_eq!(attr.key(), "test");
assert_eq!(attr.value().unwrap().contents().as_ref(), "foo");
sourcepub fn find_attribute(
&self,
name: impl AsRef<str>
) -> Result<Option<AttributeView<'_>>>
pub fn find_attribute( &self, name: impl AsRef<str> ) -> Result<Option<AttributeView<'_>>>
Returns the requested attribute.
§Example
let mut reader = PullReader::new(r#"<xml test="foo"></xml>"#.as_bytes());
let root = reader.root().unwrap();
assert_eq!(root.find_attribute("test").unwrap().unwrap().value().unwrap().contents().as_ref(), "foo");
sourcepub fn next_child<'a>(&'a mut self) -> Result<Option<Element<'a, B>>>
pub fn next_child<'a>(&'a mut self) -> Result<Option<Element<'a, B>>>
Returns the next child element of this element.
If an empty value is returned, this element has no more children.
§Example
let mut reader = PullReader::new(r#"<xml><item></item></xml>"#.as_bytes());
let mut root = reader.root().unwrap();
assert_eq!(root.next_child().unwrap().unwrap().name().as_ref(), "item");
assert_eq!(root.next_child().unwrap().is_none(), true);
sourcepub fn find_next_child<'a>(
&'a mut self,
name: impl AsRef<str>
) -> Result<Option<Element<'a, B>>>
pub fn find_next_child<'a>( &'a mut self, name: impl AsRef<str> ) -> Result<Option<Element<'a, B>>>
Returns the next child element of this element with the given tag name.
This will skip over all other tags until a match is found. If the end of the enclosing tag is reached, none is returned.
§Example
let mut reader = PullReader::new(r#"<xml><item></item><test></test></xml>"#.as_bytes());
let mut root = reader.root().unwrap();
assert_eq!(root.find_next_child("test").unwrap().unwrap().name().as_ref(), "test");
assert_eq!(root.find_next_child("test").unwrap().is_none(), true);
sourcepub fn try_text<'a>(&'a mut self) -> Result<Option<Text<'a>>>
pub fn try_text<'a>(&'a mut self) -> Result<Option<Text<'a>>>
Tries to fetch the text contents of this element.
Returns none if this element has no text at all.
§Example
let mut reader = PullReader::new(r#"<xml><item>test</item><empty></empty></xml>"#.as_bytes());
let mut root = reader.root().unwrap();
let mut item = root.next_child().unwrap().unwrap();
assert_eq!(item.try_text().unwrap().unwrap().contents().as_ref(), "test");
drop(item);
let mut test = root.next_child().unwrap().unwrap();
assert_eq!(test.try_text().unwrap().is_none(), true);
sourcepub fn text(&mut self) -> Result<Text<'_>>
pub fn text(&mut self) -> Result<Text<'_>>
Fetches the text contents of this element or returns an empty string if no text is present.
§Example
let mut reader = PullReader::new(r#"<xml><item>test</item><empty></empty></xml>"#.as_bytes());
let mut root = reader.root().unwrap();
let mut item = root.next_child().unwrap().unwrap();
assert_eq!(item.text().unwrap().contents().as_ref(), "test");
drop(item);
let mut test = root.next_child().unwrap().unwrap();
assert_eq!(test.text().unwrap().contents().as_ref(), "");
Trait Implementations§
Auto Trait Implementations§
impl<'a, B> Freeze for Element<'a, B>
impl<'a, B> !RefUnwindSafe for Element<'a, B>
impl<'a, B> !Send for Element<'a, B>
impl<'a, B> !Sync for Element<'a, B>
impl<'a, B> Unpin for Element<'a, B>
impl<'a, B> !UnwindSafe for Element<'a, B>
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
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more