<?php class BookXML extends \DOMDocument { public function filterByYear(int $year) { $books = []; $document = $this; $xpath = new DOMXPath($document); $booksObjs = $document->documentElement; $query = 'string(year)'; foreach ($booksObjs->childNodes as $booksObj) { $yearxml = $xpath->evaluate($query, $booksObj); if ($yearxml == $year) { $books[] = $booksObj; } } return $books; } } $content = ' <xml> <row> <year>2015</year> <month>september</month> </row> </xml> '; $xml = new BookXML(); $xml->loadXML($content); $filteredXML = $xml->filterByYear(2015); print_r($filteredXML);
You have javascript disabled. You will not be able to edit any code.