-
Notifications
You must be signed in to change notification settings - Fork 44
Getting Started: 7. Accessing The DOMNode
Daniele Orlando edited this page Feb 21, 2016
·
2 revisions
FluidXML wraps the DOMNode native APIs extending them but without reimplementing what is already convenient to use.
To access the node content after a query we can use the DOMNode own methods.
$chapters = $book->query('//chapter');Accessing the query result as array or iterating it returns the DOMNode unwrapped.
$last = $chapters->size() - 1;
// Raw DOMNode access.
$chapters[0]->setAttribute('first', '');
$chapters[$last]->setAttribute('last', '');foreach ($chapters as $i => $chapter) {
// $chapter is an instance of DOMNode.
$title = $chapter->nodeValue;
$id = $chapter->getAttribute('id');
$has_first_attr = $chapter->hasAttribute('first');
if ($has_first_attr) {
echo "The first chapter has title '{$title}' with id '{$id}'.\n";
} else {
$ii = $i + 1;
echo "Chapter {$ii} has title '{$title}' with id '{$id}'.\n";
}
}Pro Tip:
Many DOMNode methods and properties are available like:
hasAttribute()getAttribute()nodeValuechildNodesSee http://php.net/manual/en/class.domnode.php for the reference documentation.
Another way to retrieve the DOMNode instances is using the array() method.
$nodes = $chapters->array(); // Returns an array of DOMNode instances.