Struct elementtree::Element

source ·
pub struct Element { /* private fields */ }
Expand description

Represents an XML element.

Usually constructed from either parsing or one of the two constructors an element is part of a tree and represents an XML element and the children contained.

Imagine a structure like this:

<p>Hello <strong>World</strong>!</p>

In this case the structure is more or less represented like this:

Element {
  tag: "p",
  text: "Hello ",
  tail: None,
  children: [
    Element {
      tag: "strong",
      text: "World",
      tail: Some("!")
    }
  ]
}

Namespaces are internally managed and inherited downwards when an element is created.

Implementations§

Creates a new element without any children but a given tag.

This can be used at all times to create a new element however when you work with namespaces it’s recommended to only use this for the root element and then create further children through new_with_namespaces as otherwise namespaces will not be propagaged downwards properly.

Creates a new element without any children but inheriting the namespaces from another element.

This has the advantage that internally the map will be shared across elements for as long as no further modifications are taking place.

Parses some XML data into an Element from a reader.

Dump an element as XML document into a writer.

This will create an XML document with a processing instruction to start it. There is currently no API to only serialize a non standalone element.

Currently the writer has no way to customize what is generated in particular there is no support yet for automatically indenting elements. The reason for this is that there is no way to ignore this information automatically in the absence of DTD support which is not really planned.

Dump an element as XML document into a writer with option.

This will create an XML document with a processing instruction to start it. There is currently no API to only serialize a non standalone element.

Currently the writer has no way to customize what is generated in particular there is no support yet for automatically indenting elements. The reason for this is that there is no way to ignore this information automatically in the absence of DTD support which is not really planned.

Dump an element as XML document into a string

Returns the text of a tag.

Note that this does not trim or modify whitespace so the return value might contain structural information from the XML file.

Sets a new text value for the tag.

Returns the tail text of a tag.

The tail is the text following an element.

Sets a new tail text value for the tag.

The tag of the element as qualified name.

Use the QName functionality to extract the information from the tag name you care about (like the local name).

Sets a new tag for the element.

Returns the number of children

Returns the nth child.

Returns the nth child as a mutable reference.

Removes a child.

This returns the element if it was removed or None if the index was out of bounds.

Removes all children that don’t match a predicate.

Removes all children that don’t match a predicate. The predicate is passed a mutable reference to each element.

Appends a new child and returns a reference to self.

Appends a new child to the element and returns a reference to it.

This uses Element::new_with_namespaces internally and can then be used like this:

use elementtree::Element;

let ns = "http://example.invalid/#ns";
let mut root = Element::new((ns, "mydoc"));

{
    let mut list = root.append_new_child((ns, "list"));
    for x in 0..3 {
        list.append_new_child((ns, "item")).set_text(format!("Item {}", x));
    }
}

Returns an iterator over all children.

Returns a mutable iterator over all children.

Returns all children with the given name.

Returns all children with the given name.

Finds the first matching child

Finds the first matching child and returns a mut ref

Look up an attribute by qualified name.

Sets a new attribute.

This returns a reference to the element so you can chain the calls.

Removes an attribute and returns the stored string.

Returns an iterator over all attributes

Count the attributes

Registers a namespace with the internal namespace map.

Note that there is no API to remove namespaces from an element once the namespace has been set so be careful with modifying this!

This optionally also registers a specific prefix however if that prefix is already used a random one is used instead.

Sets a specific namespace prefix. This will also register the namespace if it was unknown so far.

In case a prefix is set that is already set elsewhere an error is returned. It’s recommended that this method is only used on the root node before other prefixes are added.

Returns the assigned prefix for a namespace.

Finds the first element that match a given path downwards

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

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

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
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.