elecena/xml-iterator

Memory efficient and fast XML parser with iterator interface

0.3.0 2024-01-15 13:21 UTC

This package is auto-updated.

Last update: 2024-12-04 21:10:34 UTC


README

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
(...)