pub struct Element {
pub prefix: Option<String>,
pub namespace: Option<String>,
pub namespaces: Option<Namespace>,
pub name: String,
pub attributes: HashMap<String, String>,
pub children: Vec<XMLNode>,
}
Expand description
Represents an XML element.
Fields§
§prefix: Option<String>
This elements prefix, if any
namespace: Option<String>
This elements namespace, if any
namespaces: Option<Namespace>
The full list of namespaces, if any
The Namespace
type is exported from the xml-rs
crate.
name: String
The name of the Element. Does not include any namespace info
attributes: HashMap<String, String>
The Element attributes
By default, this is a HashMap
, but if the optional “attribute-order” feature is enabled,
this is an IndexMap, which will retain
item insertion order.
children: Vec<XMLNode>
Children
Implementations§
Source§impl Element
impl Element
Sourcepub fn new(name: &str) -> Element
pub fn new(name: &str) -> Element
Create a new empty element with given name
All other fields are empty
Sourcepub fn parse_all<R>(r: R) -> Result<Vec<XMLNode>, ParseError>where
R: Read,
pub fn parse_all<R>(r: R) -> Result<Vec<XMLNode>, ParseError>where
R: Read,
Parses some data into a list of XMLNode
s
This is useful when you want to capture comments or processing instructions that appear before or after the root node
Sourcepub fn parse<R>(r: R) -> Result<Element, ParseError>where
R: Read,
pub fn parse<R>(r: R) -> Result<Element, ParseError>where
R: Read,
Parses some data into an Element
Sourcepub fn write<W>(&self, w: W) -> Result<(), EmitterError>where
W: Write,
pub fn write<W>(&self, w: W) -> Result<(), EmitterError>where
W: Write,
Writes out this element as the root element in an new XML document
Sourcepub fn write_with_config<W>(
&self,
w: W,
config: EmitterConfig,
) -> Result<(), EmitterError>where
W: Write,
pub fn write_with_config<W>(
&self,
w: W,
config: EmitterConfig,
) -> Result<(), EmitterError>where
W: Write,
Writes out this element as the root element in a new XML document using the provided configuration
Sourcepub fn get_child<P>(&self, k: P) -> Option<&Element>where
P: ElementPredicate,
pub fn get_child<P>(&self, k: P) -> Option<&Element>where
P: ElementPredicate,
Find a child element with the given name and return a reference to it.
Both &str
and String
implement ElementPredicate
and can be used to search for child
elements that match the given element name with .get_child("element_name")
. You can also
search by ("element_name", "tag_name")
tuple.
Note: this will only return Elements. To get other nodes (like comments), iterate through
the children
field.
Sourcepub fn get_mut_child<P>(&mut self, k: P) -> Option<&mut Element>where
P: ElementPredicate,
pub fn get_mut_child<P>(&mut self, k: P) -> Option<&mut Element>where
P: ElementPredicate,
Find a child element with the given name and return a mutable reference to it.
Sourcepub fn take_child<P>(&mut self, k: P) -> Option<Element>where
P: ElementPredicate,
pub fn take_child<P>(&mut self, k: P) -> Option<Element>where
P: ElementPredicate,
Find a child element with the given name, remove and return it.