Skip to main content

Element

Struct Element 

Source
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

Source

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

Creates a new Element with the specified tag name and no attributes or children.

§Example
use apk_info_xml::Element;

let e = Element::new("root");
assert_eq!(e.name(), "root");
Source

pub fn with_capacity(name: &str, capacity: usize) -> Element

Creates a new Element with the specified tag name and preallocated space for attributes

§Example
use apk_info_xml::Element;

let e = Element::with_capacity("root", 16);
assert_eq!(e.name(), "root");
Source

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");
Source

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)
Source

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 &amp; welcome</string>\n");
Source

pub fn append_child(&mut self, child: Element)

Appends a new child Element to this element.

§Example
use apk_info_xml::Element;

let mut root = Element::new("root");
root.append_child(Element::new("child"));
Source

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);
Source

pub fn descendants(&self) -> Descendants<'_> ⓘ

Return an iterator over all Element’s from the current root

§Example
use apk_info_xml::Element;

let mut root = Element::new("root");
let mut child = Element::new("child");
child.append_child(Element::new("subchild"));
root.append_child(child);

assert_eq!(root.descendants().count(), 2);
Source

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);
Source

pub fn name(&self) -> &str

Returns the element’s tag name.

Source

pub fn attr(&self, name: &str) -> Option<&str>

Retrieves the value of an attribute by name, if present.

§Example
use apk_info_xml::Element;

let mut e = Element::new("node");
e.set_attribute("id", "42");
assert_eq!(e.attr("id"), Some("42"));
assert_eq!(e.attr("missing"), None);

Trait Implementations§

Source§

impl Debug for Element

Source§

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

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

impl Default for Element

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display 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: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.