pub enum Xml<'a> {
Element {
name: Cow<'a, str>,
attrs: HashMap<Cow<'a, str>, Cow<'a, str>>,
children: Vec<Xml<'a>>,
},
Text(Cow<'a, str>),
}Expand description
XML node.
Variants§
Implementations§
Source§impl<'a> Xml<'a>
impl<'a> Xml<'a>
Sourcepub fn text(text: impl Into<Cow<'a, str>>) -> Self
pub fn text(text: impl Into<Cow<'a, str>>) -> Self
Create a new text node.
§Examples
let node = Xml::text("hello");
assert_eq!(node.content(), Some("hello"));Sourcepub fn element(name: impl Into<Cow<'a, str>>) -> Self
pub fn element(name: impl Into<Cow<'a, str>>) -> Self
Create a new element node.
§Examples
let node = Xml::element("div");
assert_eq!(node.name(), Some("div"));Sourcepub fn is_element(&self) -> bool
pub fn is_element(&self) -> bool
Check if the node is an element.
Sourcepub fn attr_mut(&mut self, key: &str) -> Option<&mut String>
pub fn attr_mut(&mut self, key: &str) -> Option<&mut String>
Get mutable reference to element attribute.
Sourcepub fn with_attr(
self,
key: impl Into<Cow<'a, str>>,
value: impl Into<Cow<'a, str>>,
) -> Self
pub fn with_attr( self, key: impl Into<Cow<'a, str>>, value: impl Into<Cow<'a, str>>, ) -> Self
Add attribute to element.
§Examples
let element = Xml::element("div")
.with_attr("id", "main")
.with_attr("class", "container");
assert_eq!(element.attr("id"), Some("main"));
assert_eq!(element.attr("class"), Some("container"));Sourcepub fn with_child(self, child: Self) -> Self
pub fn with_child(self, child: Self) -> Self
Add child to element.
§Examples
let element = Xml::element("div")
.with_child(Xml::text("Hello, world!"));
assert_eq!(element.children().count(), 1);
assert_eq!(element.children().next().unwrap().content(), Some("Hello, world!"));Sourcepub fn children(&self) -> Iter<'_, Xml<'_>>
pub fn children(&self) -> Iter<'_, Xml<'_>>
Iterate over direct children.
§Examples
let xml = xmlite::document("<a><b></b><c></c></a>").unwrap();
let child = xml.children().find(|e| e.name() == Some("c"));
assert_eq!(child, Some(&Xml::element("c")));Sourcepub fn children_mut<'b>(&'b mut self) -> IterMut<'b, Xml<'a>>
pub fn children_mut<'b>(&'b mut self) -> IterMut<'b, Xml<'a>>
Iterate over direct children, mutably.
Sourcepub fn descendants(&self) -> impl Iterator<Item = &Xml<'_>>
pub fn descendants(&self) -> impl Iterator<Item = &Xml<'_>>
Iterate over descendants of this node (excludes self).
Trait Implementations§
impl<'a> StructuralPartialEq for Xml<'a>
Auto Trait Implementations§
impl<'a> Freeze for Xml<'a>
impl<'a> RefUnwindSafe for Xml<'a>
impl<'a> Send for Xml<'a>
impl<'a> Sync for Xml<'a>
impl<'a> Unpin for Xml<'a>
impl<'a> UnwindSafe for Xml<'a>
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
Mutably borrows from an owned value. Read more