How can PHP differentiate between DOMElements and DOMText nodes when retrieving attributes in a DOMDocument?
When retrieving attributes in a DOMDocument, PHP can differentiate between DOMElements and DOMText nodes by checking the node type before accessing attributes. DOMElements represent elements in the DOM tree that can have attributes, while DOMText nodes represent text content within elements. By checking the node type before accessing attributes, PHP can ensure that it is working with the correct type of node.
// Assuming $node is a DOMNode object
if ($node->nodeType === XML_ELEMENT_NODE) {
// This is a DOMElement node, can access attributes
$attributeValue = $node->getAttribute('attributeName');
} elseif ($node->nodeType === XML_TEXT_NODE) {
// This is a DOMText node, handle text content accordingly
$textContent = $node->textContent;
}