pub struct Element { /* private fields */ }Expand description
Represents an XML element, including its name, attributes, and child elements.
This is the core abstraction over an XML DOM node. It provides methods for building and formatting XML trees programmatically.
§Examples
use apk_info_xml::Element;
let mut root = Element::new("root");
root.set_attribute("version", "1.0");
root.set_attribute_with_prefix(Some("xml"), "lang", "en");
let mut child = Element::new("child");
child.append_child(Element::new("grandchild"));
println!("{}", root);Implementations§
Source§impl Element
impl Element
Sourcepub fn with_capacity(name: &str, capacity: usize) -> Element
pub fn with_capacity(name: &str, capacity: usize) -> Element
Sourcepub fn set_attribute(&mut self, name: &str, value: &str)
pub fn set_attribute(&mut self, name: &str, value: &str)
Adds a new attribute without a namespace prefix to the element.
§Example
use apk_info_xml::Element;
let e = Element::new("node").set_attribute("id", "42");Sourcepub fn set_attribute_with_prefix(
&mut self,
prefix: Option<&str>,
name: &str,
value: &str,
)
pub fn set_attribute_with_prefix( &mut self, prefix: Option<&str>, name: &str, value: &str, )
Adds a new attribute with an optional namespace prefix to the element.
§Example
use apk_info_xml::Element;
let mut e = Element::new("svg");
e.set_attribute_with_prefix(Some("xlink"), "href", "image.png");
assert!(e.attributes().collect::<Vec<_>>().len() > 0)Sourcepub fn set_text(&mut self, text: &str)
pub fn set_text(&mut self, text: &str)
Sets the text content of this element (e.g. <string>value</string>).
§Example
use apk_info_xml::Element;
let mut s = Element::new("string");
s.set_attribute("name", "app_name");
s.set_text("Hello & welcome");
assert_eq!(s.to_string(), "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<string name=\"app_name\">Hello & welcome</string>\n");Sourcepub fn append_child(&mut self, child: Element)
pub fn append_child(&mut self, child: Element)
Sourcepub fn childrens(&self) -> impl Iterator<Item = &Element>
pub fn childrens(&self) -> impl Iterator<Item = &Element>
Returns an iterator over all child elements.
§Example
use apk_info_xml::Element;
let mut root = Element::new("root");
root.append_child(Element::new("child"));
assert_eq!(root.childrens().count(), 1);Sourcepub fn descendants(&self) -> Descendants<'_> ⓘ
pub fn descendants(&self) -> Descendants<'_> ⓘ
Sourcepub fn attributes(&self) -> impl Iterator<Item = &Attribute>
pub fn attributes(&self) -> impl Iterator<Item = &Attribute>
Returns an iterator over the element’s attributes.
§Example
use apk_info_xml::Element;
let mut e = Element::new("node");
e.set_attribute("id", "42");
assert_eq!(e.attributes().count(), 1);Trait Implementations§
impl Eq for Element
impl StructuralPartialEq for Element
Auto Trait Implementations§
impl Freeze for Element
impl RefUnwindSafe for Element
impl Send for Element
impl Sync for Element
impl Unpin for Element
impl UnsafeUnpin for Element
impl UnwindSafe for Element
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