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 DocumentImplementations§
Source§impl<'xml> Node<'xml>
impl<'xml> Node<'xml>
Sourcepub fn tag_name(&self) -> &str
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");pub fn tag_name_bytes(&self) -> &[u8] ⓘ
Sourcepub fn is(&self, tag_name: &str) -> bool
pub fn is(&self, tag_name: &str) -> bool
Returns true if the node’s tag name matches the provided tag name, false otherwise.
Sourcepub fn is_bytes(&self, tag_name: &[u8]) -> bool
pub fn is_bytes(&self, tag_name: &[u8]) -> bool
Returns true if the node’s tag name matches the provided byte slice, false otherwise.
Sourcepub fn text(&self) -> Option<&'xml str>
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");pub fn text_bytes(&self) -> Option<&'xml [u8]>
Sourcepub fn attributes(&self) -> Attributes<'xml> ⓘ
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");Sourcepub fn first_child_idx(&self) -> Option<NodeIdx>
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 2Sourcepub fn first_child(&self) -> Option<Node<'xml>>
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"));Sourcepub fn last_child(&self) -> Option<Node<'xml>>
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"));Sourcepub fn next_sibling(&self) -> Option<Node<'xml>>
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"));Sourcepub fn prev_sibling(&self) -> Option<Node<'xml>>
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"));Sourcepub fn children(&self) -> NodeChildren<'xml> ⓘ
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"));Sourcepub fn descendants(&self) -> Nodes<'xml> ⓘ
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();Sourcepub fn is_root(&self) -> bool
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());Sourcepub fn has_children(&self) -> bool
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());Sourcepub fn is_element(&self) -> bool
pub fn is_element(&self) -> bool
Returns true if the node is a NodeType::Element, false otherwise.
Sourcepub fn get_node_type(&self) -> &NodeType
pub fn get_node_type(&self) -> &NodeType
Returns the NodeType instance associated with this node.
Sourcepub fn get_child(&self, tag_name: &str) -> Option<Node<'xml>>
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");
}Sourcepub fn get_sibling(&self, tag_name: &str) -> Option<Node<'xml>>
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");
}Sourcepub fn get_attribute(&self, name: &str) -> Option<&'xml str>
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");
}Sourcepub fn parent(&self) -> Option<Node<'xml>>
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");
}