Node

Struct Node 

Source
pub struct Node<'xml> {
    pub idx: NodeIdx,
    pub node_info: &'xml NodeInfo,
    pub doc: &'xml Document,
}
Expand description

Represents a node in an XML document.

Node contains metadata about the node, such as its index, type, and position in the document. It provides methods to access the node’s tag name, text content, attributes, and navigation through the document tree.

Fields§

§idx: NodeIdx§node_info: &'xml NodeInfo§doc: &'xml Document

Implementations§

Source§

impl<'xml> Node<'xml>

Source

pub fn idx(&self) -> NodeIdx

Returns the index of the node in the document.

Source

pub fn tag_name(&self) -> &str

Returns the tag name of the node. If the node is not an element, it returns an empty string.

§Example
use xhtml_parser::Document;

let xml_data = b"<root><child>Text</child></root>".to_vec();
let document = Document::new(xml_data).unwrap();
let root_node = document.root().unwrap();
let tag_name = root_node.tag_name();

assert_eq!(tag_name, "root");
Source

pub fn tag_name_bytes(&self) -> &[u8]

Source

pub fn is(&self, tag_name: &str) -> bool

Returns true if the node’s tag name matches the provided tag name, false otherwise.

Source

pub fn is_bytes(&self, tag_name: &[u8]) -> bool

Returns true if the node’s tag name matches the provided byte slice, false otherwise.

Source

pub fn text(&self) -> Option<&'xml str>

Returns the text content of the node. If the node is not a text node, it returns an empty string.

§Example
use xhtml_parser::Document;

let xml_data = b"<root>The Text</root>".to_vec();
let document = Document::new(xml_data).unwrap();
let root_node = document.root().unwrap();
let child_node = root_node.first_child().unwrap();

assert!(child_node.is_text());

let text_content = child_node.text().unwrap();

assert_eq!(text_content, "The Text");
Source

pub fn text_bytes(&self) -> Option<&'xml [u8]>

Source

pub fn attributes(&self) -> Attributes<'xml>

Returns a new Attributes iterator instance for this node.

§Example
use xhtml_parser::Document;

let xml_data = b"<root name=\"The root\" id=\"1\">Text</root>".to_vec();
let document = Document::new(xml_data).unwrap();
let root_node = document.root().unwrap();
let attributes: Vec<_> = root_node.attributes().collect();

assert_eq!(attributes.len(), 2);
assert_eq!(attributes[0].name(), "name");
assert_eq!(attributes[0].value(), "The root");
assert_eq!(attributes[1].name(), "id");
assert_eq!(attributes[1].value(), "1");
Source

pub fn first_child_idx(&self) -> Option<NodeIdx>

Returns the first child index of the node, if it exists, None otherwise.

If the node has no children, it returns None. If the node is in forward-only mode, it returns the next index that is not a sibling of the current node.

§Example
use xhtml_parser::Document;

let xml_data = b"<root><child1/><child2/></root>".to_vec();
let document = Document::new(xml_data).unwrap();
let root_node = document.root().unwrap();
let first_child_idx = root_node.first_child_idx();

assert_eq!(first_child_idx, Some(2)); // Assuming the first child is at index 2
Source

pub fn first_child(&self) -> Option<Node<'xml>>

Returns the first child of the node, if it exists, None otherwise.

§Example
use xhtml_parser::Document;

let xml_data = b"<root><child1/><child2/></root>".to_vec();
let document = Document::new(xml_data).unwrap();
let node = document.root().unwrap();    

assert!(node.first_child().unwrap().is("child1"));
Source

pub fn last_child(&self) -> Option<Node<'xml>>

Returns the last child of the node, if it exists, None otherwise.

§Example
use xhtml_parser::Document;

let xml_data = b"<root><child1/><child2/></root>".to_vec();
let document = Document::new(xml_data).unwrap();
let root_node = document.root().unwrap();
let last_child = root_node.last_child().unwrap();

assert!(last_child.is("child2"));
Source

pub fn next_sibling(&self) -> Option<Node<'xml>>

Returns the next sibling of the node, if it exists, None otherwise.

§Example
use xhtml_parser::Document;

let xml_data = b"<root><child1/><child2/></root>".to_vec();
let document = Document::new(xml_data).unwrap();
let root_node = document.root().unwrap();
let next_sibling = root_node.first_child().unwrap().next_sibling().unwrap();

assert!(next_sibling.is("child2"));
Source

pub fn prev_sibling(&self) -> Option<Node<'xml>>

Returns the previous sibling of the node, if it exists, None otherwise.

§Example
use xhtml_parser::Document;

let xml_data = b"<root><child1/><child2/></root>".to_vec();
let document = Document::new(xml_data).unwrap();
let root_node = document.root().unwrap();
let prev_sibling = root_node.last_child().unwrap().prev_sibling().unwrap();

assert!(prev_sibling.is("child1"));
Source

pub fn children(&self) -> NodeChildren<'xml>

Returns an iterator over the children of the node. If the node has no children, it returns an empty iterator.

§Example
use xhtml_parser::Document;

let xml_data = b"<root><child1/><child2/></root>".to_vec();
let document = Document::new(xml_data).unwrap();
let root_node = document.root().unwrap();
let children: Vec<_> = root_node.children().collect();

assert_eq!(children.len(), 2);
assert!(children[0].is("child1"));
assert!(children[1].is("child2"));
Source

pub fn descendants(&self) -> Nodes<'xml>

Returns an iterator over all descendants of the node.

This includes all children, grandchildren, and so on. If the node has no descendants, it returns an empty iterator.

§Example
use xhtml_parser::Document;

let xml_data = b"<root><child1><subchild/></child1><child2/></root>".to_vec();
let document = Document::new(xml_data).unwrap();
let root_node = document.root().unwrap();
let descendants: Vec<_> = root_node.descendants().collect();
Source

pub fn is_root(&self) -> bool

Returns true if the node is the root node, false otherwise.

§Example
use xhtml_parser::Document;

let xml_data = b"<root><child/></root>".to_vec();
let document = Document::new(xml_data).unwrap();
let root_node = document.root().unwrap();

assert!(root_node.is_root());
Source

pub fn has_children(&self) -> bool

Returns true if the node has children, false otherwise.

§Example
use xhtml_parser::Document;

let xml_data = b"<root><child/></root>".to_vec();
let document = Document::new(xml_data).unwrap();
let root_node = document.root().unwrap();

assert!(root_node.has_children());
Source

pub fn is_element(&self) -> bool

Returns true if the node is a NodeType::Element, false otherwise.

Source

pub fn is_text(&self) -> bool

Returns true if the node is a NodeType::Text, false otherwise.

Source

pub fn get_node_type(&self) -> &NodeType

Returns the NodeType instance associated with this node.

Source

pub fn get_child(&self, tag_name: &str) -> Option<Node<'xml>>

Finds a child node with the specified tag name. If the node has no children, it returns None.

§Example
use xhtml_parser::Document;

let xml_data = b"<root><child1/><child2/></root>".to_vec();
let document = Document::new(xml_data).unwrap();
let root_node = document.root().unwrap();

if let Some(child) = root_node.get_child("child2") {
    assert!(child.is("child2"));
} else {
    panic!("Child node not found");
}
Source

pub fn get_sibling(&self, tag_name: &str) -> Option<Node<'xml>>

Finds a sibling node with the specified tag name. If the node has no parent or no siblings, it returns None.

§Example
use xhtml_parser::Document;

let xml_data = b"<root><child1/><child2/></root>".to_vec();
let document = Document::new(xml_data).unwrap();
let root_node = document.root().unwrap();
let child_node = root_node.first_child().unwrap();

if let Some(sibling) = child_node.get_sibling("child2") {
    assert!(sibling.is("child2"));
} else {
    panic!("Sibling node not found");
}
Source

pub fn get_attribute(&self, name: &str) -> Option<&'xml str>

searches for an attribute by name and returns its value if found.

§Example
use xhtml_parser::Document;

let xml_data = b"<root name=\"value\">Text</root>".to_vec();
let document = Document::new(xml_data).unwrap();
let root_node = document.root().unwrap();

if let Some(value) = root_node.get_attribute("name") {
    assert_eq!(value, "value");
} else {
    panic!("Attribute not found");
}
Source

pub fn parent(&self) -> Option<Node<'xml>>

Returns the parent node of this node, if it exists. If this node is the root node, it returns None.

§Example
use xhtml_parser::Document;

let xml_data = b"<root><child>Text</child></root>".to_vec();
let document = Document::new(xml_data).unwrap();
let root_node = document.root().unwrap();
let child_node = root_node.first_child().unwrap();

if let Some(parent) = child_node.parent() {
    assert!(parent.is("root"));
} else {
    panic!("Child node has no parent");
}
Source

pub fn position(&self) -> XmlIdx

Returns the position of this node in the XML source.

Trait Implementations§

Source§

impl<'xml> Clone for Node<'xml>

Source§

fn clone(&self) -> Node<'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 Node<'xml>

Source§

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

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

impl PartialEq for Node<'_>

Source§

fn eq(&self, other: &Self) -> 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 Eq for Node<'_>

Auto Trait Implementations§

§

impl<'xml> Freeze for Node<'xml>

§

impl<'xml> RefUnwindSafe for Node<'xml>

§

impl<'xml> Send for Node<'xml>

§

impl<'xml> Sync for Node<'xml>

§

impl<'xml> Unpin for Node<'xml>

§

impl<'xml> UnwindSafe for Node<'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.