Skip to main content

AnyElement

Struct AnyElement 

Source
pub struct AnyElement<'xml> {
    pub ns: Cow<'xml, str>,
    pub name: Cow<'xml, str>,
    pub attributes: Vec<AnyAttribute<'xml>>,
    pub text: Option<Cow<'xml, str>>,
    pub children: Vec<AnyElement<'xml>>,
}
Expand description

A dynamically captured XML element.

The AnyElement type captures an arbitrary XML element tree at runtime, preserving its namespace, name, attributes, text content, and nested children. This is useful when the XML schema allows arbitrary content (e.g. <xs:any namespace="##any" processContents="skip" />) or when the element structure is not known at compile time.

String values are borrowed from the input XML where possible via Cow.

§Example

As root:

use instant_xml::{from_str, AnyElement};

let xml = r#"<item xmlns="http://example.com" key="val">hello</item>"#;
let elem: AnyElement<'_> = from_str(xml).unwrap();

assert_eq!(elem.name, "item");
assert_eq!(elem.ns, "http://example.com");
assert_eq!(elem.text.as_deref(), Some("hello"));
assert_eq!(elem.attributes.len(), 1);

As child (borrowing from the input):

use instant_xml::{from_str, FromXml, AnyElement};

#[derive(Debug, FromXml, PartialEq)]
#[xml(ns("http://example.com"))]
struct Wrapper<'a> {
    #[xml(borrow)]
    inner: AnyElement<'a>,
}

let xml = r#"<Wrapper xmlns="http://example.com"><item>text</item></Wrapper>"#;
let parsed: Wrapper<'_> = from_str(xml).unwrap();

assert_eq!(parsed.inner.name, "item");
assert_eq!(parsed.inner.ns, "http://example.com");
assert_eq!(parsed.inner.text.as_deref(), Some("text"));

Note: When using AnyElement as a field in a derived struct, add #[xml(borrow)] so the derive macro generates the correct lifetime bounds. Use into_owned() to convert to AnyElement<'static> when you need to decouple from the input lifetime.

§Matching behavior

AnyElement matches any XML element regardless of namespace or name. When used as a field in a derived struct, it will capture whichever element appears in that position. For capturing multiple arbitrary children, use Vec<AnyElement>.

Fields§

§ns: Cow<'xml, str>

XML namespace URI of this element.

§name: Cow<'xml, str>

Local element name.

§attributes: Vec<AnyAttribute<'xml>>

Attributes on this element.

§text: Option<Cow<'xml, str>>

Text content of this element, if any.

§children: Vec<AnyElement<'xml>>

Nested child elements.

Implementations§

Source§

impl<'a> AnyElement<'a>

Source

pub fn into_owned(self) -> AnyElement<'static>

Converts this element into an owned version with 'static lifetime.

This recursively converts all borrowed strings into owned copies, decoupling the result from the original XML input.

Trait Implementations§

Source§

impl<'xml> Clone for AnyElement<'xml>

Source§

fn clone(&self) -> AnyElement<'xml>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'xml> Debug for AnyElement<'xml>

Source§

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

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

impl<'xml, 'a> FromXml<'xml> for AnyElement<'a>
where 'xml: 'a,

Source§

fn matches(_id: Id<'_>, _field: Option<Id<'_>>) -> bool

Matches any element regardless of namespace or name.

Source§

const KIND: Kind = Kind::Element

The kind of XML node this type represents
Source§

type Accumulator = Option<AnyElement<'a>>

The accumulator type used during deserialization
Source§

fn deserialize<'cx>( into: &mut Self::Accumulator, _field: &'static str, deserializer: &mut Deserializer<'cx, 'xml>, ) -> Result<(), Error>

Deserialize from XML into an accumulator
Source§

impl<'xml> PartialEq for AnyElement<'xml>

Source§

fn eq(&self, other: &AnyElement<'xml>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'xml> Eq for AnyElement<'xml>

Source§

impl<'xml> StructuralPartialEq for AnyElement<'xml>

Auto Trait Implementations§

§

impl<'xml> Freeze for AnyElement<'xml>

§

impl<'xml> RefUnwindSafe for AnyElement<'xml>

§

impl<'xml> Send for AnyElement<'xml>

§

impl<'xml> Sync for AnyElement<'xml>

§

impl<'xml> Unpin for AnyElement<'xml>

§

impl<'xml> UnsafeUnpin for AnyElement<'xml>

§

impl<'xml> UnwindSafe for AnyElement<'xml>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> FromXmlOwned for T
where T: for<'xml> FromXml<'xml>,