elecena / xml-iterator

Memory efficient and fast XML parser with iterator interface
https://packagist.org/packages/elecena/xml-iterator
MIT License
0 stars 0 forks source link
iterator php-library xml-parser

xml-iterator

Memory efficient and fast XML parser with the iterator interface.

Usage example

Getting the list of sub-sitemaps in the remote XML sitemap file.

use Elecena\XmlIterator\XMLParser;
use Elecena\XmlIterator\Nodes\XMLNodeOpen;
use Elecena\XmlIterator\Nodes\XMLNodeContent;

require 'vendor/autoload.php';

$stream = fopen('https://elecena.pl/sitemap.xml', mode: 'rt');

foreach(new XMLParser($stream) as $node) {
    if ($node instanceof XMLNodeContent && $node->name === 'loc') {
        echo "Sub-sitemap found: {$node->content}\n";
    }
    elseif ($node instanceof XMLNodeOpen && $node->name === 'sitemapindex') {
        echo "Sitemap index node found, attributes: " . print_r($node->attributes, return: true) . "\n";
    }
}

fclose($stream);

The above will give you:

Sitemap index node found: Array
(
    [xmlns] => http://www.sitemaps.org/schemas/sitemap/0.9
)

Sub-sitemap found: https://elecena.pl/sitemap-001-search.xml.gz
Sub-sitemap found: https://elecena.pl/sitemap-002-shops.xml.gz
Sub-sitemap found: https://elecena.pl/sitemap-003-pages.xml.gz
Sub-sitemap found: https://elecena.pl/sitemap-004-datasheets.xml.gz
Sub-sitemap found: https://elecena.pl/sitemap-005-datasheets.xml.gz
Sub-sitemap found: https://elecena.pl/sitemap-006-datasheets.xml.gz
(...)