Struct xml_doc::Element[][src]

pub struct Element { /* fields omitted */ }
Expand description

Represents an XML element. It acts as a pointer to actual element data stored in Document.

This struct only contains a unique usize id and implements trait Copy. So you do not need to bother with having a reference.

Because the actual data of the element is stored in Document, most methods takes &Document or &mut Document as its first argument.

Note that an element may only interact with elements of the same document, but the crate doesn’t know which document an element is from.

If you for example attempt to call .remove_child_elem() with elements from other document, unexpected errors may occur, or may panic. You also can’t move elements between documents.

Implementations

Create a new empty element with full_name.

If full_name contains :, everything before that will be interpreted as a namespace prefix.

Chain methods to build an element easily. The chain can be finished with .finish() or .push_to(parent).

Example
use xml_doc::{Document, Element, Node};

let mut doc = Document::new();

let elem = Element::build(&mut doc, "root")
    .attribute("id", "main")
    .attribute("class", "main")
    .finish();

doc.push_root_node(elem.as_node());

Returns true if element is a container.

See Document::container() for more information on ‘container’.

Equivalent to Node::Element(self)

Seperate full_name by :, returning (prefix, name).

The first str is "" if full_name has no prefix.

Below are methods that take &Document as its first argument.

Returns true if this element is the root node of document.

Note that this crate allows Document to have multiple elements, even though it’s not valid xml.

Get full name of element, including its namespace prefix. Use Element::name() to get its name without the prefix.

Get prefix and name of element. If it doesn’t have prefix, will return an empty string.

<prefix: name -> ("prefix", "name")

Get namespace prefix of element, without name.

<prefix:name> -> "prefix"

Set prefix of element, preserving its name.

prefix should not have a :, or everything after : will be interpreted as part of element name.

If prefix is an empty string, removes prefix.

Get name of element, without its namespace prefix. Use Element::full_name() to get its full name with prefix.

<prefix:name> -> "name"

Set name of element, preserving its prefix.

name should not have a :, or everything before : may be interpreted as namespace prefix.

Get attributes of element.

The attribute names may have namespace prefix. To strip the prefix and only its name, call Element::separate_prefix_name.

use xml_doc::{Document, Element};

let mut doc = Document::new();
let element = Element::build(&mut doc, "name")
    .attribute("id", "name")
    .attribute("pre:name", "value")
    .finish();

let attrs = element.attributes(&doc);
for (full_name, value) in attrs {
    let (prefix, name) = Element::separate_prefix_name(full_name);
    // ("", "id"), ("pre", "name")
}

Add or set attribute.

If name contains a :, everything before : will be interpreted as namespace prefix.

Gets the namespace of this element.

Shorthand for self.namespace_for_prefix(doc, self.prefix(doc)).

Gets HashMap of xmlns:prefix=namespace declared in this element’s attributes.

Default namespace has empty string as key.

Get namespace value given prefix, for this element. “xml” and “xmlns” returns its default namespace.

Concatenate all text content of this element, including its child elements text_content().

Implementation of Node.textContent

Clears all its children and inserts a Node::Text with given text.

Below are methods related to finding nodes in tree.

self.parent(doc).is_some()

Get child Nodes of this element.

Get all child nodes recursively. (i.e. includes its children’s children.)

!self.children(doc).is_empty()

Get only child Elements of this element.

This calls .children().iter().filter_map().collect(). Use Element::children() if performance is important.

Get child Elements recursively. (i.e. includes its child element’s child elements)

Find first direct child element with name name.

Find all direct child element with name name. If you care about performance, call self.children().iter().filter()

Below are functions that modify its tree-structure.

Because an element has reference to both its parent and its children, an element’s parent and children is not directly exposed for modification. But in return, it is not possible for a document to be in an inconsistant state, where an element’s parent doesn’t have the element as its children.

Errors

These errors are shared by below methods.

  • Error::HasAParent: When you want to replace an element’s parent with another, call element.detatch() to make it parentless first. This is to make it explicit that you are changing an element’s parent, not adding another.
  • Error::ContainerCannotMove: The container element’s parent must always be None.

Equivalent to vec.push().

Equivalent to parent.push_child().

Equivalent to vec.insert().

Panics

Panics if index > self.children().len()

Equivalent to vec.remove().

Panics

Panics if index >= self.children().len().

Remove child element by value.

Errors

Remove all children

Removes itself from its parent. Note that you can’t attach this element to other documents.

Errors

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.