pub struct ElementRef<'s> {
pub name: &'s str,
pub text_content: Cow<'s, str>,
pub attributes: IndexMap<&'s str, &'s str>,
pub children: Vec<ElementRef<'s>>,
}
Expand description
An XML element.
This is a result of zero-copy parsing, meaning you might run into lifetime issues.
If you need to own the element separately from the source XML, you can
convert it to Element
using
into_owned
.
Fields§
§name: &'s str
The name of the element, e.g. LuaComponent
in <LuaComponent />
.
text_content: Cow<'s, str>
The text content of the element, e.g. hello
in
<SomeComponent>hello</SomeComponent>
.
If there are multiple text nodes, they are concatenated into a single string with spaces between them. This is the only case where the parsing is not zero-copy, as the text is discontinuous in the source XML.
If there is no text content, the value is Cow::Borrowed("")
.
attributes: IndexMap<&'s str, &'s str>
A map of element attributes, e.g. name="comp"
in <SomeComponent name="comp" />
, where the key is name
and the value is comp
.
children: Vec<ElementRef<'s>>
A list of child elements, e.g. [<SomeComponent/>
,
<SomeOtherComponent/>
] in
<Entity>
<SomeComponent/>
<SomeOtherComponent/>
</Entity>
Implementations§
Source§impl<'s> ElementRef<'s>
impl<'s> ElementRef<'s>
Source§impl<'s> ElementRef<'s>
impl<'s> ElementRef<'s>
Sourcepub fn attr(&self, key: &str) -> Option<&str>
pub fn attr(&self, key: &str) -> Option<&str>
A shorthand for getting an attribute value.
§Example
let element = nxml_ref!(<Entity key="value"/>);
assert_eq!(element.attr("key"), Some("value"));
Sourcepub fn child(&self, name: &str) -> Option<&Self>
pub fn child(&self, name: &str) -> Option<&Self>
Find the first child element with the given name.
§Example
let element = nxml_ref!(<Entity><Child>"hello"</Child></Entity>);
assert_eq!(element.child("Child").unwrap().text_content, "hello");
Sourcepub fn child_mut(&mut self, name: &str) -> Option<&mut Self>
pub fn child_mut(&mut self, name: &str) -> Option<&mut Self>
Find the first child element with the given name, mutable version.
§Example
let mut element = nxml_ref!(<Entity><Child/></Entity>);
element.child_mut("Child").unwrap().text_content = "world".into();
assert_eq!(element.child("Child").unwrap().text_content, "world");
Sourcepub fn children<'a>(
&'a self,
name: &'a str,
) -> impl Iterator<Item = &'a Self> + 'a
pub fn children<'a>( &'a self, name: &'a str, ) -> impl Iterator<Item = &'a Self> + 'a
Iterate over all child elements with the given name.
§Example
let element = nxml_ref!(<Entity><Child/><Other/><Child/></Entity>);
assert_eq!(element.children("Child").count(), 2);
Sourcepub fn children_mut<'a>(
&'a mut self,
name: &'a str,
) -> impl Iterator<Item = &'a mut Self> + 'a
pub fn children_mut<'a>( &'a mut self, name: &'a str, ) -> impl Iterator<Item = &'a mut Self> + 'a
Iterate over all child elements with the given name, mutable version.
§Example
let mut element = nxml_ref!(<Entity><Child/><Other/><Child/></Entity>);
for child in element.children_mut("Child") {
child.text_content = "text".into();
}
assert_eq!(element.to_string(), "<Entity><Child>text</Child><Other/><Child>text</Child></Entity>");
Sourcepub fn set_attr(&mut self, key: &'s str, value: &'s str)
pub fn set_attr(&mut self, key: &'s str, value: &'s str)
A shorthand for setting an attribute value.
§Example
let mut element = nxml_ref!(<Entity />);
element.set_attr("key", "value");
assert_eq!(element.to_string(), "<Entity key=\"value\"/>");
Sourcepub fn remove_attr(&mut self, key: &str) -> Option<&'s str>
pub fn remove_attr(&mut self, key: &str) -> Option<&'s str>
A shorthand for removing an attribute value.
§Example
let mut element = nxml_ref!(<Entity key="value" other="other" />);
element.remove_attr("key");
assert_eq!(element.to_string(), "<Entity other=\"other\"/>");
Sourcepub fn with_text(self, text: &'s str) -> Self
pub fn with_text(self, text: &'s str) -> Self
Chained shorthand for setting the text content.
§Example
let element = ElementRef::new("Entity")
.with_text("hello");
assert_eq!(element.to_string(), "<Entity>hello</Entity>");
Sourcepub fn with_child(self, element: Self) -> Self
pub fn with_child(self, element: Self) -> Self
Chained shorthand for adding a child element.
§Example
let element = ElementRef::new("Entity")
.with_child(ElementRef::new("Child"));
assert_eq!(element.to_string(), "<Entity><Child/></Entity>");
Sourcepub fn display(&self) -> PrettyDisplay<'_, Self>
pub fn display(&self) -> PrettyDisplay<'_, Self>
Trait Implementations§
Source§impl<'s> Clone for ElementRef<'s>
impl<'s> Clone for ElementRef<'s>
Source§fn clone(&self) -> ElementRef<'s>
fn clone(&self) -> ElementRef<'s>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<'s> Debug for ElementRef<'s>
impl<'s> Debug for ElementRef<'s>
Source§impl<'s> Display for ElementRef<'s>
impl<'s> Display for ElementRef<'s>
Source§impl<'s, 'e> Div<&str> for &'e ElementRef<'s>
impl<'s, 'e> Div<&str> for &'e ElementRef<'s>
Source§fn div(self, rhs: &str) -> Self::Output
fn div(self, rhs: &str) -> Self::Output
A chainable child element accessor
§Example
let element = nxml_ref!(<Entity><Child><Grandchild>"hello"</Grandchild></Child></Entity>);
assert_eq!(&element / "Child" / "Grandchild" % Text, "hello");
Source§type Output = &'e ElementRef<'s>
type Output = &'e ElementRef<'s>
/
operator.Source§impl<'s, 'e> Div<&str> for &'e mut ElementRef<'s>
impl<'s, 'e> Div<&str> for &'e mut ElementRef<'s>
Source§fn div(self, rhs: &str) -> Self::Output
fn div(self, rhs: &str) -> Self::Output
A mutable version of the child accessor.
§Example
let mut element = nxml_ref!(<Entity><Child><Grandchild>hello</Grandchild></Child></Entity>);
(&mut element / "Child").children.clear();
assert_eq!(element.to_string(), "<Entity><Child/></Entity>");
Source§type Output = &'e mut ElementRef<'s>
type Output = &'e mut ElementRef<'s>
/
operator.Source§impl<'s> PartialEq for ElementRef<'s>
impl<'s> PartialEq for ElementRef<'s>
Source§impl<'s, 'e> Rem<&str> for &'e ElementRef<'s>
impl<'s, 'e> Rem<&str> for &'e ElementRef<'s>
Source§impl<'s, 'e> Rem<Text> for &'e ElementRef<'s>
impl<'s, 'e> Rem<Text> for &'e ElementRef<'s>
impl<'s> Eq for ElementRef<'s>
impl<'s> StructuralPartialEq for ElementRef<'s>
Auto Trait Implementations§
impl<'s> Freeze for ElementRef<'s>
impl<'s> RefUnwindSafe for ElementRef<'s>
impl<'s> Send for ElementRef<'s>
impl<'s> Sync for ElementRef<'s>
impl<'s> Unpin for ElementRef<'s>
impl<'s> UnwindSafe for ElementRef<'s>
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
Source§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string()
Read moreSource§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString
. Read more